我正在将一个控制台程序与一个包含文件名行(相对于当前目录)的配置文件移植到 Qt。当我在 QtCreator 中运行/调试 GUI 时,似乎无法从工作目录加载文件。
假设我的文件系统如下:
C:/MyProjectSource
C:/MyProjectSource-Desktop-Debug // <-- created by QtCreator
C:/MyProjectSource-Desktop-Debug/debug/MyProgram.exe // <- my program
C:/MyProjectSource-Desktop-Debug/debug/params.txt // <- program's config
C:/MyProjectSource-Desktop-Debug/debug/cfg/file1.txt // <- extra file
C:/MyProjectSource-Desktop-Debug/debug/cfg/file2.txt // <- extra file
params.txt 看起来像
# other setting
...
cfg/file1.txt # at this line, program crash, saying cannot open file
cfg/file2.txt
...
# other setting
读取file1.txt的代码
std::ifstream ifs(filename); // <-- @runtime, filename = 'cfg/file1.txt'
if (!ifs)
{
// failed inside QtCreator, but fine when start from explorer
EMSG_OPENFILE(filename);
exit(-1);
}
基线:尽可能不更改控制台程序。那么,如何告诉 QtCreator 理解相对路径呢?还是我应该在 params.txt 中强制执行绝对路径?谢谢。
平台:QtCreator 2.6.1、Qt4.8、MinGW64、Win 7