3
 QFile file("test.txt");
 if (file.open(QIODevice::ReadOnly)) {
     qDebug()<<"You got me.";
 }

I am using:

  • Qt 4.8.6 with MSVC 2010
  • Qt Creator 3.1.1
  • Windows 7 (32 bit)

From the above code, if .pro file has not been changed, the corresponding build directory

for the debug mode:

D:\...\build-Main-MSVC2010-Debug

and the .exe of the debug mode will be located in

D:\...\build-Main-MSVC2010-Debug\debug

for the release mode:

D:\...\build-Main-MSVC2010-Release

and the .exe of the release mode will be located in

D:\...\build-Main-MSVC2010-Release\release

[Question]

If I want the release program to read the "test.txt" file, I put the file in the folder

D:\...\build-Main-MSVC2010-Release\release

which makes sense.

But if I want the debug program to read the "test.txt" file, I have to put the file in the folder

D:\...\build-Main-MSVC2010-Debug\

rather than

D:\...\build-Main-MSVC2010-Debug\debug

I am wondering why the relative file path worked differently in debug & release mode, it has been bothering me for a long time.


[Edit]

Thanks for @Paul and @lpapp. Here is the screenshot of working directory:

Debug: enter image description here

Release: enter image description here


[Very important Edit]

For @Paul and @lpapp:

I used to copy required .dll to the release folder to test runtime, and I just found if I execute the release program through Qt Creator, the working directory will work just like what you said. But if I directly click the .exe in the release folder, the situation will be as what I said in the question. I think there might be differences between running program from Qt Creator and directly executing the program.

Community
  • 1
  • 1
Tay2510
  • 5,748
  • 7
  • 39
  • 58
  • 1
    QFile file("test.txt"); -> hmm. I think it is better to use [applicationDirPath + "text.txt"](http://doc-snapshot.qt-project.org/qt5-5.4/qcoreapplication.html#applicationDirPath). I think the reason behind is the current working directory when running the executable. Perhaps you can post the two screenshots in QtCreator so that we can see they are similar? – László Papp Dec 16 '14 at 09:15
  • Very appreciate, lpapp. Even though not answering the question, using `QCoreApplication::applicationDirPath ()` solves everything out. – Tay2510 Dec 16 '14 at 09:44
  • 1
    Oh, I have just noticed your edit. Indeed, the difference is that the working directory is not guaranteed to be the same, depending on where you run it from. – László Papp Dec 16 '14 at 10:33
  • Thanks, I think I'll just use `applicationDirPath()` in the future. It's much safer anyway. – Tay2510 Dec 16 '14 at 10:41

1 Answers1

1

This depends on current working directory of your program. You can change it in Project->Run settings->Run->Working directory.

enter image description here

if I execute the release program through Qt Creator, the working directory will work just like what you said. But if I directly click the .exe in the release folder, the situation will be as what I said in the question.

This is because when you click the .exe in the release foder, that folder is the working directory for the program. When you run the program from Qt Creator, Qt Creator sets the working directory explicitly. You may override the Working directory to be the directory where .exe file is created so it'll be no difference whether you run the program from Qt Creator or just click the .exe in Explorer.

Paul
  • 13,042
  • 3
  • 41
  • 59