c++ - How to check why file is not opened? -
std::ifstream fin; fin.open("file.txt", std::ios::in); std::cout << fin.is_open();
this code prints false
file not opened. how check why it. maybe error message fin
object? guessing reason file opened writing. want open reading. possible open file both reading , writing.?
i using linux. strerror(errno)
error no such file or directory file exists. maybe error because opened using other object? opened using c api, therefore fin
cant open it. how can open file opened?
the standard specifies there preprocessor symbol errno
, expands references (thread local) int
, in system functions expected put extended error code. defines function strerror
, allow recovering char const*
text message, given number (but unlike errno
, function not guaranteed thread safe).
the standard not place requirements on filebuf
use errno
, @ least not in case of open
, in practice, filebuf
invoke lower level functions (hopefully) use errno
. (this required posix, of system calls set errno
in case of error. i'm less sure windows.)
as bi-directional access: it's bad idea, but... filebuf
potentially bidirectional, ifstream
won't define <<
operators, , ofstream
won't define >>
operators, might want use std::fstream
. if want able seek, you'll want open file in binary mode; otherwise, can seek places you've been before, , memorized position. if want read you've written, better solution close output, open reading.
Comments
Post a Comment