0

Simple C++ blank project in Qt IDE. I want to open file. It works only with full path.

Works:

fstream file;
file.open("C:\\Users\\Me\\Documents\\Cat\\in.txt", ios::in);

Doesn't work:

fstream file;
file.open("in.txt", ios::in);

The file is in the same directory as the whole project.

  • 1
    Try it with the file in the same place the exe is. – NathanOliver May 19 '16 at 18:06
  • I put this file to build directory, where exe is, and it still does not work. – John Kokon May 19 '16 at 18:10
  • Check the setting for working directory in your IDE. – molbdnilo May 19 '16 at 18:12
  • I don't know the QT IDE, but most IDEs play fun games with the program's working directory--the directory in which the program will run and may have absolutely nothing to do with where the program itself is or your files are. You will probably find a setting in the IDE's configuration pages that will show you and allow you to set the working directory or [you can use getcwd](http://man7.org/linux/man-pages/man2/getcwd.2.html) (_getcwd in windows) to track down the working directory. – user4581301 May 19 '16 at 18:13
  • 1
    If you want to get cutting edge, you can also give [std::experimental::filesystem::current_path](http://en.cppreference.com/w/cpp/experimental/fs/current_path) a try. – user4581301 May 19 '16 at 18:13
  • http://stackoverflow.com/questions/143174/how-do-i-get-the-directory-that-a-program-is-running-from That was helpful. Working dir for Qt is C:/User/Name/Documents/build-project At first I put in.txt in C:/User/Name/Documents/build-project/debug – John Kokon May 19 '16 at 18:35
  • Add: `std::cout << "CWD: " << get_current_dir_name() << "\n";` to see what directory your application thinks it is running in. Or see [How to get Current Directory?](http://stackoverflow.com/q/875249/14065) for info – Martin York May 19 '16 at 19:35

0 Answers0