1.函數功能:
檢查調用進程是否可以對指定的文件執行某種操作。
2.函數原型:
1)函數頭文件
[cpp] view plain copy
#include <stdio.h>
#include <unistd.h>
2)函數
[cpp] view plain copy
int access(const char * pathname, int mode)
3)形參
pathname:需要檢測的文件路勁名
mode:需要測試的操作模式。
4)函數返回值說明
成功執行時,返回0。失敗返回-1,errno被設爲以下的某個值
EINVAL: 模式值無效
EACCES: 文件或路徑名中包含的目錄不可訪問
ELOOP : 解釋路徑名過程中存在太多的符號連接
ENAMETOOLONG:路徑名太長
ENOENT:路徑名中的目錄不存在或是無效的符號連接
ENOTDIR: 路徑名中當作目錄的組件並非目錄
EROFS: 文件系統只讀
EFAULT: 路徑名指向可訪問的空間外
EIO:輸入輸出錯誤
ENOMEM: 不能獲取足夠的內核內存
ETXTBSY:對程序寫入出錯
5)mode說明
[cpp] view plain copy
R_OK 測試讀許可權
W_OK 測試寫許可權
X_OK 測試執行許可權
F_OK 測試文件是否存在
3.函數實例
[cpp] view plain copy
#include <stdio.h>
#include <unistd.h>
int main(void)
{
if(access("test.txt", R_OK)==0) printf("READ OK\n");
if(access("test.txt", W_OK)==0) printf("WRITE OK\n");
if(access("test.txt", X_OK)==0) printf("EXEC OK\n");
if(access("test.txt", F_OK)==0) printf("File exist\n");
}