2016-02-28 44 views
-1

例如,我在我當前的工作目錄下有目錄a和b。我試圖找到文件X,我如何修改stat()命令,以便它檢查目錄a和b而不是僅當前的工作目錄? stat(a/file, &buf)會工作嗎?另外,要檢查它是否可執行,我知道代碼是buf.S_IXUSR,請問if (buf.S_IXUSR)是否工作?檢查是否存在並可在C中執行,使用stat函數

謝謝!

+0

你試過了嗎? – Evert

+0

是的,我有,我無法弄清楚如何讓它瀏覽不同的目錄,這就是爲什麼我問:/ – chenchen

回答

1

我建議你參考stat(2)手冊頁。

這裏是stat如何使用的例子:

struct stat buf; 

if (stat("a/file", &buf) != 0) { 
    // handle failure 
} 

// Check the `st_mode` field to see if the `S_IXUSR` bit is set 
if (buf.st_mode & S_IXUSR) { 
    // Executable by user 
} 

然而,對於你的使用情況,您可以考慮access(2)代替:

if (access("/path/to/file", X_OK) == 0) { 
    // File exists and is executable by the calling process's 
    // _real_ UID/GID. 
} 
+0

啊,我看到了!我正在瀏覽'man stat',不知道有'man 2 stat',謝謝! – chenchen

+0

再次嗨!我得到了一切爲當前目錄工作,但我仍然在努力讓它適用於子目錄,如dir a和b,'stat(「a/file」,&buf)'似乎不工作,因爲它總是說沒有這樣的文件,即使該文件存在 – chenchen

+0

「a」是程序當前工作目錄的子目錄嗎? –

相關問題