'getline' was not declared in this scope, what does it mean? - qnx

When compile, I have 'getline' was not declared in this scope
compiling next code:
string PostCmd::getFailedFromFile()
{
string failedList;
ifstream file(FILE, ifstream::in);
if (file.is_open())
{
getline(file, failedList);
file.close();
}
else
{
cout << "Unable to open file";
}
return failedList;
}

Try to include <istream>.
More info can be seen here: http://www.qnx.com/developers/docs/6.5.0_sp1/index.jsp?topic=%2Fcom.qnx.doc.dinkum_en_ecpp%2Fistream.html

Related

Why does this zig program fail to compile due to "expected error union type, found 'error:124:18'"?

test "error union if" {
var ent_num: error{UnknownEntity}!u32 = error.UnknownEntity;
if (ent_num) |entity| {
try expect(#TypeOf(entity) == u32);
try expect(entity == 5);
} else |err| {
_ = err catch |err1| { // compiles fine when this block is removed
std.debug.print("{s}", .{err1});
};
std.debug.print("{s}", .{err});
}
}
./main.zig:125:5: error: expected error union type, found 'error:124:18'
if (ent_num) |entity| {
^
./main.zig:129:17: note: referenced here
_ = err catch |err1| {
error:124:18 refers to the error{UnknownEntity} because it's
anonymous. so prints the definition address.
the if (my_var) |v| ... syntax can only be used for optional
types. for Error unions you must use try or catch.
try and catch can't be used for Error Set
your code would be this:
const std = #import("std");
const expect = std.testing.expect;
test "error union if" {
var ent_num: error{UnknownEntity}!u32 = error.UnknownEntity;
const entity: u32 = ent_num catch |err| {
std.debug.print("{s}", .{err});
return;
};
try expect(#TypeOf(entity) == u32);
try expect(entity == 5);
}
reading the programs or libraries is very useful for learning a new language. gl

Want to check IF File.Exists to define StreamReader, to then searching for specific line

I don't write code often and my knowledge is still low-level. I need help for something that I can't figure out
public static void SearchScriptEnd()
{
int counter = 0;
string line;
Report.Log(ReportLevel.Info, "Read Log", "Starting to find: Test Suite Ended");
var text = "Test Suite Ended";
if (File.Exists(ConfigController.Home + TestSuite.Current.Parameters["LogPath"]))
{
StreamReader file =
new StreamReader(ConfigController.Home + TestSuite.Current.Parameters["LogPath"]);
}else{
StreamReader file =
new StreamReader(TestSuite.Current.Parameters["LogPath"]);
}
while ((line = file.ReadLine()) != null)
{
if (line.Contains(text))
{
Report.Log(ReportLevel.Info, "Read Log", "[Success] Script End String has been found");
Report.Log(ReportLevel.Info, "Read Log", string.Format("Line number: '{0}'", counter));
return;
}
counter++;
}
Report.Log(ReportLevel.Failure, "Read Log", "[Missing] Anvil Script End String NOT found");
file.Close();
}
At first, the while was in both statement and was working well, but I want to use the While outside of that statement, but I'm getting The name 'file' does not exist in the current context (CS0103) and I don't know how to get the value of file out of my If statement.
You need to get the variable out of the if-scope. Try this:
StreamReader file;
if (File.Exists(ConfigController.Home + TestSuite.Current.Parameters["LogPath"]))
{
file = new StreamReader(ConfigController.Home + TestSuite.Current.Parameters["LogPath"]);
}else{
file = new StreamReader(TestSuite.Current.Parameters["LogPath"]);
}
That way you have a value in file for sure. Happy coding.

Vala get file modification date

I'm new to Vala and linux programming in general.
Im am trying to enumerate the data similar to the 'stat' shell utility for a given folder.
So far it's this i got:
int main (string[] args) {
try {
File directory = File.new_for_path (".");
if (args.length > 1) {
directory = File.new_for_commandline_arg (args[1]);
}
FileEnumerator enumerator = directory.enumerate_children (FileAttribute.TIME_MODIFIED, 0);
FileInfo file_info;
while ((file_info = enumerator.next_file ()) != null) {
DateTime t = file_info.get_modification_date_time();
}
} catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
return 1;
}
return 0;
}
Console output:
vala --pkg gio-2.0 --pkg glib-2.0 main3.vala
main3.vala:16.24-16.59: error: The name `get_modification_date_time' does not exist in the context of `GLib.FileInfo?'
Could someone point me in the right direction?
Thanks.
The error is saying the method doesn't exist. Looking at Valadoc.org for get_modification_date_time it shows this was introduced in GLib version 2.62. That version was released 05 September 2019. It is likely your distribution doesn't include that release yet.
You can either try to update your version of GLib or use the now deprecated get_modification_time:
int main(string[] args) {
if (args[1] == null) {
stderr.printf("No filename given\n");
return 1;
}
var file = GLib.File.new_for_path (args[1]);
try {
GLib.FileInfo info = file.query_info("*", FileQueryInfoFlags.NONE);
print (info.get_modification_time().to_iso8601() + "\n");
print ("\n\nFull info:\n");
foreach (var item in info.list_attributes (null)) {
print( #"$item - $(info.get_attribute_as_string (item))\n" );
}
} catch (Error error) {
stderr.printf (#"$(error.message)\n");
return 1;
}
return 0;
}

exception thrown on cancelation when using ctrl+c

pplx::task<void> RestInterface::Getsxxxx()
{
utility::stringstream_t ss;
pplx::cancellation_token_source cts;
pplx::cancellation_token ct = cts.get_token();
ss<<"http://"<<URL<<":"<<PORT<<PATH<<ZALL;
http_client client(U(ss.str()));
///here were using the jSON function for URI to request the REST API
///else the name of the API can be passed to stream also.
http_request requestJson(methods::GET);
requestJson.headers().set_content_type(U("application/json"));
///you must perform the request
///against a server that provides JSON data.
return client.request(requestJson, ct).then([=](http_response response) -> pplx::task<web::json::value>
{
///If the status is OK extract the body of the response into a JSON value
if(!getCancelledSate())
{
if (response.status_code() == status_codes::OK)
{
return response.extract_json();
}
else
{
// return an empty JSON value
isConnected = false;
return pplx::task_from_result(web::json::value());
}
}
else
{
//try{
cts.cancel();
//}
//catch(http_exception const & e)
//{
// std::cout<<__FILE__<<"["<<__LINE__<<"]"<<"Exception:"<< e.what() << std::endl;
//}
}
}, ct).then([=](pplx::task<web::json::value> previousTask)
{
try{
std::cout <<" before previousTask.wait()"<< std::endl;
///Get the JSON value from the task and call the traversal method
if(previousTask.wait() == pplx::task_status::canceled){
//Do nothing
std::cout <<" after previousTask.wait()"<< std::endl;
}else{
web::json::value const & dataValue = previousTask.get();
m_dataJson = dataValue;
///clear string streambuffer
m_Buffer.str("");
m_Buffer.clear();
m_Buffer << m_dataJson.serialize();
utility::string_t streamJson = m_Buffer.str();
if((strxxxxcmp(streamJson, APP_ACTIVE) < 0)){
isConnected = false;
//......So work
}else{
//......So work
}
}
}
catch(http_exception const & e)
{
isConnected = false;
std::cout<<__FILE__<<"["<<__LINE__<<"]"<<"Exception:"<< e.what() << std::endl;
}
});
}
void RestInterface::xxxxz()
{
try
{
isConnected = true;
Getsxxxx().wait();
}
catch(const std::exception &e)
{
std::cout<<__FILE__<<"["<<__LINE__<<"]"<<"Exception:"<< e.what() << std::endl;
//printf("Error exception:%s\n", e.what());
}
}
Now i have a problem. we have a scenario where we have to cancel the Task when a signal is generated like for e.g. when SIGINT is generated...
when user gives SIGINT using Ctrl+C Sometimes i can see the below message.
**/build/casablanca-FHxOgj/casablanca-2.8.0/Release/include/pplx/pplxtasks.h:1835: pplx::task_status pplx::details::_Task_impl_base::_Wait(): Assertion `_IsCompleted()' failed.
Aborted**
i think that some task is throwing an exception that is not handled, but i am not entirely sure which one and about the reason. Can anybody share some thoughts on it.
any help will be great

vala FileInfo get_file_type is UNKNOWN

I've been trying to make a function that goes through a directory and lists all the files in the directory and any sub-directories:
void get_listing (string dir) {
try {
var directory = File.new_for_path (dir);
var enumerator = directory.enumerate_children (FILE_ATTRIBUTE_STANDARD_NAME, 0);
FileInfo file_info;
while ((file_info = enumerator.next_file ()) != null) {
stdout.printf(file_info.get_file_type().to_string());
if (file_info.get_file_type() == FileType.DIRECTORY) {
get_listing(file_info.get_name());
} else {
stdout.printf ("%s\n", file_info.get_name ());
}
}
} catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
return;
}
}
int main (string[] args) {
get_listing(".");
return 0;
}
When I run this code none of the files in any sub-directories are outputted. All the files/directories types are "G_FILE_TYPE_UNKNOWN". Does anyone know how to fix this or another method I could use.
You are enumerating files by name only; if you wish to access file type later, you should pass appropriate hint to enumerator:
FILE_ATTRIBUTE_STANDARD_NAME + "," + FILE_ATTRIBUTE_STANDARD_TYPE

Resources