linux C函数之access函数的用法

1.函数功能:api

检查调用进程是否能够对指定的文件执行某种操做。微信

2.函数原型:ide

1)函数头文件函数


[cpp] view plain copy测试

  1. #include <stdio.h>  url

  2. #include <unistd.h>  spa



2)函数.net

[cpp] view plain copyblog

  1. 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

  1. R_OK      测试读许可权  

  2. W_OK      测试写许可权  

  3. X_OK      测试执行许可权  

  4. F_OK      测试文件是否存在  

3.函数实例

[cpp] view plain copy

  1. #include <stdio.h>  

  2. #include <unistd.h>  

  3.   

  4. int main(void)  

  5. {  

  6. if(access("test.txt", R_OK)==0)  printf("READ OK\n");  

  7.  if(access("test.txt", W_OK)==0)  printf("WRITE OK\n");  

  8.  if(access("test.txt", X_OK)==0)  printf("EXEC OK\n");  

  9.  if(access("test.txt", F_OK)==0)   printf("File exist\n");  

  10. }  

相关文章
相关标签/搜索