linux access函数检测文件属性

在linux环境下使用access函数可以获取文件的属性信息(是否存在,是否可读,是否可写,是否可执行)。

1. 相关头文件

#include <unistd.h>

2.access 函数原型

/***********************************
** description : 检测文件的属性
** parameter:
	** pathname:需要检测的文件名字
	** mode:检测该文件是否具备的属性,参数值如下:
		** F_OK:是否存在
		** R_OK:是否可读
		** W_OK:是否可写
		** X_OK:是否可执行
** return:检测属性有效 成功:0, 失败:-1
************************************/
int access(const char *pathname, int mode);

3.举例

int main(int argc, char *argv[]{
	char *file_path_name = "/home/linux/text.txt";	
	
	if (access(file_path_name, F_OK) == 0) 
	{
		fprintf(stdout, "file exist, ");
	}
	else 
	{
		fprintf(stdout, "not exist, ", name);
		return -1;
	}
	
	if (access(file_path_name, R_OK) == 0)
	{
		fprintf(stdout, "has read premission, ");
	} 
	else 
	{
		fprintf(stdout, "does not have read premission, ");
	}

	if (access(file_path_name, W_OK) == 0)
	{
		fprintf(stdout, "has write premission, ");
	} 
	else 
	{
		fprintf(stdout, "does not have write premission, ");
	}

	if (access(file_path_name, X_OK) == 0) 
	{
		fprintf(stdout, "has execute premission\n");
	}
	else 
	{
		fprintf(stdout, "does not have execute premission\n");
	}

	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值