問題描述
我正在使用 Qt 庫用 C++ 編寫程序.我的主 bin 目錄中有一個指向可執行文件的符號鏈接.我希望我的程序的當前工作目錄是我的終端所在的目錄(即 pwd
命令的結果).我看到了 QDir::currentPath()
函數,但它返回了二進制文件所在的目錄.
I'm writing a program in C++ using the Qt library. There is a symbolic link in my home bin directory to the executable. I would like the current working directory of my program to be the directory in which I am with my terminal (ie. the result of the pwd
command). I saw the QDir::currentPath()
function, but it gives back the directory where the binary is.
如何找到我當前的工作目錄?
How can I find my current working directory?
推薦答案
感謝 RedX 和 Kaz 的回答.我不明白為什么我會給出exe的路徑.我找到了另一種方法:
Thank you RedX and Kaz for your answers. I don't get why by me it gives the path of the exe. I found an other way to do it :
QString pwd("");
char * PWD;
PWD = getenv ("PWD");
pwd.append(PWD);
cout << "Working directory : " << pwd << flush;
它不如單行優雅......但它對我有用.
It is less elegant than a single line... but it works for me.
這篇關于獲取 Qt 應用程序中的當前工作目錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!