2
我正在將一個配置文件(包含文件名(相對於當前目錄))的配置文件移植到Qt。當我在QtCreator中運行/調試GUI時,似乎無法從工作目錄加載文件。QtCreator中的相對路徑
假設我的文件系統如下:
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的