zoukankan      html  css  js  c++  java
  • 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、

  • 相关阅读:
    cf492D Vanya and Computer Game
    cf492C Vanya and Exams
    cf492A Vanya and Cubes
    bzoj2038 [2009国家集训队]小Z的袜子(hose)
    bzoj3781 小B的询问
    bzoj1858 [Scoi2010]序列操作
    bzoj1060 [ZJOI2007]时态同步
    算法学习心得
    bzoj1054 [HAOI2008]移动玩具
    bzoj3437 小P的牧场
  • 原文地址:https://www.cnblogs.com/cppskill/p/10069819.html
Copyright © 2011-2022 走看看