引言:
access函数主要用于文件读取方面——判断文件是否存在,并判断文件是否可写。Linux下,该函数为access,位于头文件<unistd.h>中,而在标准C++中,该函数为_access,位于头文件<io.h>中,两者的使用方法基本相同,只是在一些参数方面可能会有一些不同的宏定义。
windows环境
windows下函数原型:
头文件:<io.h>
函数原型:int _access(const char *pathname, int mode);
参数:pathname 为文件路径或目录路径 mode 为访问权限(在不同系统中可能用不能的宏定义重新定义)
返回值:如果文件具有指定的访问权限,则函数返回0;如果文件不存在或者不能访问指定的权限,则返回-1.
备注:当pathname为文件时,_access函数判断文件是否存在,并判断文件是否可以用mode值指定的模式进行访问。当pathname为目录时,_access只判断指定目录是否存在,在Windows NT和Windows 2000中,所有的目录都只有读写权限。
mode的值和含义如下所示:
- 00——只检查文件是否存在
- 02——写权限
- 04——读权限
- 06——读写权限
 对应的还有- _access的宽字符版本,用法相同。
windows下英文原文定义:
access
Synopsis:
  #include
  int _access(const char *path,int mode);
Description
  The access function, when used with files, determines whether the specified file exists and can be accessed as specified by the value of mode. When used with directories, _access determines only whether the specified directory exists; since under Windows all directories have read and write access.
The mode argument can be one of :
  00 Existence only
  02 Write permission
  04 Read permission
  06 Read and write permission
Returns
  Zero if the file has the given mode, -1 if an error occurs.
Portability :
  Windows. Under Unix a similar function exists too.
  Note that lcc-win32 accepts both _access (Microsoft convention) and access.
Windows下代码实例:
该段代码未做验证,请读者自行验证:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <io.h>
#include <map>
#include <math.h>
#include <list>
#include <string>
#include <sstream>
#include <algorithm>// std::find(...)
#include <vector>
using namespace std;
//#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <math.h>
using namespace std;
void main()
{
    int iRtn = _access("D:/G_资料_2018/20181119_xx", 0);// 这个文件夹是 存在的
    printf("_access return(1) : %d\n", iRtn);
    iRtn = _access("D:/G_资料_2018/20181119_xx_No", 0);// 这个文件夹是 不存在的
    printf("_access return(2) : %d\n", iRtn);
    iRtn = _access("D:/G_资料_2018/20181119_xx/芜湖两条线路/华二112线.g", 0);// 这个文件是 存在的
    printf("_access return(3) : %d\n", iRtn);
    iRtn = _access("D:/G_资料_2018/20181119_xx/芜湖两条线路/华二112线.no.g", 0);// 这个文件是 不存在的
    printf("_access return(4) : %d\n", iRtn);
    system("pause");
}
Linux环境
Linux下函数原型:
函数功能:检查调用进程是否可以对指定的文件执行某种操作。
函数头文件:
| 1 | #include <stdio.h> | 
函数
| 1 | int access(const char * pathname, int mode) | 
形参
pathname:需要检测的文件路劲名
mode:需要测试的操作模式。
函数返回值说明
成功执行时,返回0。失败返回-1,errno被设为以下的某个值:
- EINVAL: 模式值无效
- EACCES: 文件或路径名中包含的目录不可访问
- ELOOP: 解释路径名过程中存在太多的符号连接
- ENAMETOOLONG:路径名太长
- ENOENT:路径名中的目录不存在或是无效的符号连接
- ENOTDIR: 路径名中当作目录的组件并非目录
- EROFS: 文件系统只读
- EFAULT: 路径名指向可访问的空间外
- EIO:输入输出错误
- ENOMEM: 不能获取足够的内核内存
- ETXTBSY:对程序写入出错
 mode说明
| 参数名 | 功用 | 
|---|---|
| R_OK | 测试读许可权 | 
| W_OK | 测试写许可权 | 
| X_OK | 测试执行许可权 | 
| F_OK | 测试文件是否存在 | 
Linux下代码实例:
//main.cpp
#include <stdio.h>
#include <unistd.h>
int main(void) {
  if (access("test.txt", R_OK) == 0) printf("READ OK\n");
  if (access("test.txt", W_OK) == 0) printf("WRITE OK\n");
  if (access("test.txt", X_OK) == 0) printf("EXEC OK\n");
  if (access("test.txt", F_OK) == 0) {
    printf("File exist\n");
  } else {
    printf("File doesn't exist\n");
  }
  return 0;
}
编译运行结果
patten@patten-hp:~/workspace/others/cpp/demo$ g++ main.cpp -std=c++11
patten@patten-hp:~/workspace/others/cpp/demo$ ./a.out 
File doesn't exist
patten@patten-hp:~/workspace/others/cpp/demo$





