C/C++.判断文件是否存在(_access)

1、

int _access(char* path,int mode)
头文件<io.h>
功能:确定文件或文件夹的访问权限。如果指定的存取方式有效,则函数返回0,否则函数返回-1。

参数path 是访问文件所在的路径名,mode是访问判断模式,
具体含义如下:
  R_OK 只判断是否有读权限
  W_OK 只判断是否有写权限
  X_OK 判断是否有执行权限
  F_OK 只判断是否存在

之前也使用过fopen判断文件是否存在,但_access函数更为方便。

2、代码: 环境:Win7x64,vs08x86

#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");
}

 

3、

 

4、

5、

 

posted @ 2018-12-05 11:14  CppSkill  阅读(14799)  评论(0编辑  收藏  举报