zoukankan      html  css  js  c++  java
  • Linux access

    1.access函数

    功能描述:检查调用进程是否可以对指定的文件执行某种操作.

    用法:

    #include <unistd.h>

    #include <fcntl.h>
    int access(const char *pathname, int mode);  

    参数:

    pathname: 需要测试的文件路径名.

    mode: 需要测试的操作模式,可能值是一个或多个R_OK(可读?), W_OK(可写?), X_OK(可执行?) 或 F_OK(文件存在?)组合体.   

    返回说明:

    成功执行时,返回0.失败返回-1,errno被设为以下的某个值

    EINVAL:模式值无效  

    EACCES: 文件或路径名中包含的目录不可访问

    ELOOP : 解释路径名过程中存在太多的符号连接

    ENAMETOOLONG:路径名太长

    ENOENT:  路径名中的目录不存在或是无效的符号连接

    ENOTDIR: 路径名中当作目录的组件并非目录

    EROFS: 文件系统只读

    EFAULT: 路径名指向可访问的空间外

    EIO:  输入输出错误

    ENOMEM: 不能获取足够的内核内存

    ETXTBSY:对程序写入出错

    //For example:

    linux 判断文件是否存在的access()示例:

      1. #include <stdio.h> 
      2. #include <stdlib.h> 
      3. #include <unistd.h> 
      4. #include <fcntl.h> 
      5.  
      6. int main() 
      7.     if((access("test.c",F_OK))!=-1) 
      8.     { 
      9.         printf("文件 test.c 存在. "); 
      10.     } 
      11.     else 
      12.     { 
      13.         printf("test.c 不存在! "); 
      14.     } 
      15.  
      16.     if(access("test.c",R_OK)!=-1) 
      17.     { 
      18.         printf("test.c 有可读权限"); 
      19.     } 
      20.     else 
      21.     { 
      22.         printf("test.c 不可读. "); 
      23.     } 
      24.  
      25.     if(access("test.c",W_OK)!=-1) 
      26.     { 
      27.         printf("test.c 有可写权限"); 
      28.     } 
      29.     else 
      30.     { 
      31.         printf("test.c 不可写. "); 
      32.     } 
      33.     if(access("test.c",X_OK)!=-1) 
      34.     { 
      35.         printf("test.c 有可执行权限"); 
      36.     } 
      37.     else 
      38.     { 
      39.         printf("test.c 不可执行. "); 
      40.     } 
      41.  
      42.     return 0; 
  • 相关阅读:
    android之Fragment(官网资料翻译)
    Java获取当前时间的年月日方法
    Android Fragment Base
    Java JNI初探
    PHP 初学之登录查询小case
    PHP 初学
    tomcat:run和tomcat7:run的区别,以及Apache Tomcat Maven Plugin 相关
    Intellij IDEA:maven的本地仓库问题
    Java 构造方法的执行过程(猜测)
    PHP wamp server问题
  • 原文地址:https://www.cnblogs.com/AlwaysOnLines/p/5584562.html
Copyright © 2011-2022 走看看