#include <unistd.h>
int access(const char *pathname, int mode);
成功返回0
失败返回-1
R_OK(可读)、W_OK(可写)、X_OK(可执行)
具体示例如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
if(argc < 2){
printf("args error\n");
exit(1);
}
int ret = access(argv[1], W_OK);
if(ret==-1){
perror("access");
exit(1);
}
printf("你可以写这个文件\n");
return 0;
}
测试执行结果:
yu@ubuntu:~/cplusplus/wjcz_文件操作$ ls -l
total 36
-rw-rw-r-- 1 yu yu 288 Dec 16 14:33 access.c
-rwxrwxr-x 1 yu yu 8760 Dec 16 14:33 a.out
-rw-rw-r-- 1 yu yu 1821 Dec 16 13:46 ls-l.c
-rwxrwxr-x 1 yu yu 9192 Dec 16 14:01 myls
-rw-rw-r-- 1 yu yu 65 Dec 16 11:06 stat.c
yu@ubuntu:~/cplusplus/wjcz_文件操作$ gcc access.c -o xaccess
yu@ubuntu:~/cplusplus/wjcz_文件操作$ ./xaccess access.c
你可以写这个文件