Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ifstream open file C++

Tags:

c++

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

like image 886
JuP Avatar asked Oct 07 '12 14:10

JuP


People also ask

Does ifstream open file?

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.

What does open () function do in C++?

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);


Video Answer


1 Answers

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.

like image 178
Julian Avatar answered Oct 09 '22 08:10

Julian