Checking if a file opened successfully with ifstream

You can simply do this: int devices::open_file(std::string _file_name) { ifstream input_stream; input_stream.open(_file_name.c_str(), ios::in); if(!input_stream) { return -1; } file_name = _file_name; return 0; } fail() is not a static method, you must call it on an instance not a type, so if you want to use fail(), replace !input_stream with input_stream.fail() in my code above. … Read more

ifstream::is_open vs ifstream::fail?

INTRODUCTION std::ifstream::fail includes checking std::ifstream::is_open, but std::ifstream::is_open only checks if it was possible to create a handle to the file. EXPLANATION std::ifstream::fail can return true, even if std::ifstream::is_open returns true; they are not the mutually exclusive. .fail will check the overall “health” of the stream, which involves things such as checking the stream has currently … Read more

Why can’t I return std::getline’s as-if-boolean result?

The boolean conversion operator for std::basic_istream is explicit. This means that instances of the type will not implicitly become a bool but can be converted to one explicitly, for instance by typing bool(infile). Explicit boolean conversion operators are considered for conditional statements, i.e. the expression parts of if, while etc. More info about contextual conversions … Read more

How does ifstream’s eof() work?

-1 is get‘s way of saying you’ve reached the end of file. Compare it using the std::char_traits<char>::eof() (or std::istream::traits_type::eof()) – avoid -1, it’s a magic number. (Although the other one is a bit verbose – you can always just call istream::eof) The EOF flag is only set once a read tries to read past the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)