JuP
JuP

Reputation: 535

ifstream open file 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

Upvotes: 5

Views: 3820

Answers (1)

Julian
Julian

Reputation: 852

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.

Upvotes: 6

Related Questions