1. 程式人生 > 其它 >LINUX下C語言用access()檢查檔案可讀、可寫許可權

LINUX下C語言用access()檢查檔案可讀、可寫許可權

技術標籤:Linux/ShellC/C++

  有時還是很有必要的:

#include <unistd.h>
#include <fcntl.h>

int access(const char *pPath, int mode);   

F_OK:檔案是否存在
R_OK:讀
W_OK:寫
X_OK:執行

#define LOCK_FILE "quantum6.lock"

    if (access(LOCK_FILE, W_OK) != -1)   
    {   
        printf("%s can write.\n", LOCK_FILE);   
    }   
    else  
    {   
        printf("%s can not write.\n", LOCK_FILE);   
    }