Difference in relative file path: debug mode and r

2019-05-18 09:58发布

问题:

 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:

Release:


[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.

回答1:

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

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.