函数原型:
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;
}