linux查看文件权限函数,linux下用access函数判断文件读写可执行权限

检查当前进程对指定的文件是否具有执行某种操作的权限。

函数用法:

#include

#include

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

函数参数:

pathname: 需要检测的文件路径。

mode: 需要测试的操作模式,赋的值是一个或者多个,多个用“|”(与操作)取R_OK(可读?), W_OK(可写?), X_OK(可执行?) 或 F_OK(文件存在?)的组合体。

/* Values for the second argument to access.

These may be OR'd together. */

#defineR_OK4/* Test for read permission. */

#defineW_OK2/* Test for write permission. */

#defineX_OK1/* Test for execute permission. */

#defineF_OK0/* Test for existence. */

函数返回值:

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

EINVAL: 模式值无效

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

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

ENAMETOOLONG:路径名太长

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

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

EROFS: 文件系统只读

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

EIO: 输入输出错误

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

ETXTBSY:对程序写入出错

函数实例:

#include

#include

#include

#include

int main(int argc, char *argv[])

{

if (argc < 2) {

printf("Usage: ./test filename\n");

exit(1);

}

if (access(argv[1], F_OK) == -1) {

puts("File not exists!");

exit(2);

}

if (access(argv[1], R_OK) == -1)

puts("You can't read the file!");

else

if (access(argv[1], R_OK | W_OK) != -1)

puts("You can read and write the file");

else

puts("You can read the file");

exit(0);

}

除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值