2012-04-20 30 views
0

我得到一個錯誤與此istruction:opendir錯誤?

dp = opendir ("%APPDATA%/."); 

    output: 
    Couldn't open directory: Mo such file or directory. 

,但我不同意這種istruction得到一個埃羅:

dp = opendir ("C:/Users/xrobot/AppData/."); 

output: 
. 
.. 
Local 
LocalLow 
Roaming 

爲什麼?

回答

7

opendir不會展開像%APPDATA%這樣的元變量,shell會這樣做。所以這樣的事情在命令行中起作用,但不是來自程序。在你的程序中,你必須使用絕對路徑或相對路徑。

你也許可以得到與getenv()所需的路徑,

const char *appData = getenv("APPDATA"); 
if (appData) { 
    dp = opendir(appData); 
} else { 
    /* die or recover */ 
} 
+0

那麼,我怎樣才能使用%APPDATA%爲我的c + +程序? 謝謝 – xRobot 2012-04-20 12:37:23

+0

我不是Windows專家,但是您可能必須使用'getenv()'。嘗試'const char * appData = getenv(「APPDATA」); dp = opendir(appData);' – 2012-04-20 12:40:01

+0

我不知道C++,在C中你可以用'getenv'來查找'APPDATA'的值。那麼,至少在* nixish系統上,不確定Windows。 – 2012-04-20 12:40:24

2

因爲第一opendirLITERALLY試圖打開該目錄%APPDATA%/.