i have problem with ifstream open function. I create app under linux in netbeans. My code is:
ifstream file;
file.open(path);
file.is_open()
.
.
.
and problem is in path. When I use ~/Desktop/file.txt and run app, file is not opened. But when I debug the app, all works fine. Any hints for this problem? Thanks
std::ifstream::open. Opens the file identified by argument filename , associating it with the stream object, so that input/output operations are performed on its content. Argument mode specifies the opening mode. If the stream is already associated with a file (i.e., it is already open), calling this function fails.
Opening a File And ifstream object is used to open a file for reading purpose only. Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects. void open(const char *filename, ios::openmode mode);
The pathname ~/Desktop/file.txt will not match a file unless it has had the tilde character expanded, which is usually done by the shell before passing it to the program. If you are calling it directly, then you need to use either a full pathname
/home/user/Desktop/file.txt
or a relative path
./Desktop/file.txt
I suspect the debugger is expanding the file name for you to be helpful before passing it to the program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With