• 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
    ", iRtn);
        iRtn = _access("D:/G_资料_2018/20181119_xx_No", 0);// 这个文件夹是 不存在的
        printf("_access return(2) : %d
    ", iRtn);
    
        iRtn = _access("D:/G_资料_2018/20181119_xx/芜湖两条线路/华二112线.g", 0);// 这个文件是 存在的
        printf("_access return(3) : %d
    ", iRtn);
        iRtn = _access("D:/G_资料_2018/20181119_xx/芜湖两条线路/华二112线.no.g", 0);// 这个文件是 不存在的
        printf("_access return(4) : %d
    ", iRtn);
    
        system("pause");
    }

    3、

    4、

    5、

  • 相关阅读:
    iphone4 wifi超时设置修改
    siteminder sso agent 初探
    [读书]35前要掌握的66种基本能力序
    Secrets of Successful Project Management
    成功项目管理的秘密
    一位乞丐给我上的MBA课程
    [读书]35前要掌握的66种基本能力第2节
    简单,有规律
    网盘使用手记
    更好地领导一个项目的诀窍
  • 原文地址:https://www.cnblogs.com/cppskill/p/10069819.html
Copyright © 2020-2023  润新知