5

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

JuP
  • 535
  • 1
  • 6
  • 23

1 Answers1

6

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.

Julian
  • 852
  • 4
  • 9
  • ./Desktop/file.txt is not working. But It have to work with ~ too. – JuP Oct 07 '12 at 14:44
  • It depends a lot on how you are invoking the command, and from which directory too. – Julian Oct 07 '12 at 14:46
  • 2
    The correct directory to use for `~` is in the environment variable `HOME` which you get with `getenv("HOME")`. So if the file name you get starts with `~/` you just replace the tilde with the content of `HOME` in order to get the absolute path. Note however that you should generally *not* do that with file names you get from the command line, because they are expected to be tilde expanded by the shell. – celtschk Oct 07 '12 at 14:48