判断文件是否存在的另一种方法 _access 和 _waccess

函数原型:

int _access( const char *path, int mode );

int _waccess( const wchar_t *path, int mode );


示例代码:

#include <io.h>
#include <stdio.h>
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
{
	//如果文件具有指定的访问权限,则函数返回0
	//如果文件不存在或者不能访问指定的权限,则返回-1

	//备注
	//当path为文件时,_access函数判断文件是否存在,并判断文件是否可以用mode值指定的模式进行访问
	//当path为目录时,_access只判断指定的目录是否存在,在WindowsNT和Windows2000中,所有目录都有读写权限

	//mode值
	//00	只检查文件是否存在
	//02	写权限
	//04	读权限
	//06	读写权限

	//_waccess是_access的宽字符版本
	if (_access("demo.txt", 0) != -1)
	{
		printf("the demo.txt exist\n");

		//判断文件是否可写,假定文件是只读的
		if (_access("demo.txt", 2) == -1)
		{
			printf("the demo.txt does not have write permission\n");
		}
		else
		{
			printf("the demo.txt have write permission\n");
		}
	}
	else
	{
		printf("the demo.txt does not exist\n");
	}

	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hellokandy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值