转载本文是为了实际之需要,方便查阅。

一、判断文件是否存在

#ifdef WIN32
#include <io.h>                      //C (Windows)    access
#else
#include <unistd.h>                  //C (Linux)      access
#endif#include <fstream>                   //C++           fstream#ifdef WIN32
#include <Windows.h>                 //Windows API   FindFirstFile
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")  //Windows API   PathFileExists
#endif#include <boost/filesystem.hpp>      //boost
using namespace std;int main()
{char file_name[] = "D://aa.txt";//C     run in windows and linuxif ( 0 == access(file_name, 0) )  cout<<"access(): file exist."<<endl;else                              cout<<"access(): file not exist."<<endl;//C++     run in windows and linuxfstream fs;fs.open(file_name, ios::in);if (fs)   cout<<"fstream: file exist."<<endl;else      cout<<"fstream: file not exist."<<endl;fs.close();//Windows API     run in windows
#ifdef WIN32WIN32_FIND_DATA wfd;HANDLE hFind = FindFirstFile(file_name, &wfd);if ( INVALID_HANDLE_VALUE != hFind )   cout<<"FindFirstFile: file exist."<<endl;else                                   cout<<"FindFirstFile: file not exist."<<endl; CloseHandle(hFind);if ( PathFileExists(file_name) )       cout<<"PathFileExists: file exist."<<endl;else                                   cout<<"PathFileExists: file not exist."<<endl;if ( INVALID_FILE_ATTRIBUTES != GetFileAttributes(file_name) )   cout<<"GetFileAttributes: file exist."<<endl;else                                                             cout<<"GetFileAttributes: file not exist."<<endl;if ( INVALID_HANDLE_VALUE != CreateFile(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, NULL, NULL) )  cout<<"CreateFile: file exist."<<endl;else                                                                                                    cout<<"CreateFile: file not exist."<<endl;
#endif//boost      run in windows and linuxboost::filesystem::path path_file(file_name);if ( boost::filesystem::exists(path_file) && boost::filesystem::is_regular_file(path_file) )   cout<<"boost: file exist."<<endl;else                                                   cout<<"boost: file not exist."<<endl;return 0;
}

二、判断目录是否存在

#ifdef WIN32
#include <io.h>                      //C (Windows)    access
#else
#include <unistd.h>                  //C (Linux)      access
#endif#ifdef WIN32
#include <Windows.h>                 //Windows API   FindFirstFile
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")  //Windows API   PathFileExists
#endif#include <boost/filesystem.hpp>      //boost
using namespace std;int main()
{char file_name[] = "D://b";//C     run in windows and linuxif ( 0 == access(file_name, 0) )  cout<<"access(): path exist."<<endl;else                              cout<<"access(): path not exist."<<endl;//Windows API     run in windows
#ifdef WIN32WIN32_FIND_DATA wfd;HANDLE hFind = FindFirstFile(file_name, &wfd);if ( INVALID_HANDLE_VALUE != hFind && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )   cout<<"FindFirstFile: path exist."<<endl;else                                                                                        cout<<"FindFirstFile: path not exist."<<endl; CloseHandle(hFind);if ( PathFileExists(file_name) )       cout<<"PathFileExists: path exist."<<endl;else                                   cout<<"PathFileExists: path not exist."<<endl;if ( INVALID_FILE_ATTRIBUTES != GetFileAttributes(file_name) )   cout<<"GetFileAttributes: path exist."<<endl;else                                                             cout<<"GetFileAttributes: path not exist."<<endl;
#endif//boost      run in windows and linuxboost::filesystem::path path_file(file_name);if ( boost::filesystem::exists(path_file) && boost::filesystem::is_directory(path_file) )      cout<<"boost: path exist."<<endl;else                                                   cout<<"boost: path not exist."<<endl;return 0;
}

三、几种方式比较
1. 精确判断文件和目录的

FindFirstFile()            (Windows API,  Windows)

boost                           (boost,  Windows and Linux)

2. 不精确判断文件盒目录的

access()                     (C ,  Windows and Linux)

PathFileExists()         (Windows API ,  Windows)

GetFileAttributes()     (Windows API,  Windows)

3. 只能判断文件的

fstream                       (C++ STL,  Windows and Linux)

CreateFile()                (Windows API,  Windows)

————————————————
版权声明:本文为CSDN博主「kanguolaikanguolaik」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/guowenyan001/article/details/17259173

【C、C++、Windows API、 boost】多种方式判断文件、目录是否存在相关推荐

  1. Java多种方式读文件,追加文件内容,等对文件的各种操作

    一.多种方式读文件内容.1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内容 4.随机读取文件内容 import java.io.BufferedReader;import java. ...

  2. 多种方式读取文件内容

    2019独角兽企业重金招聘Python工程师标准>>> import java.io.*; /**  *  * @author wxp  * Created by Administr ...

  3. Shell中判断文件,目录是否存在

    一. 具体每个选项对应的判断内容: -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filena ...

  4. windows API和cuda方式读取显卡信息

    windows API 方式读取到的显卡信息,可以去DirectX或者设备装置器中查看 代码: #include <Windows.h> #include <iostream> ...

  5. shell判断文件,目录是否存在或者具有权限 (转载)

    转自:http://cqfish.blog.51cto.com/622299/187188 文章来源:http://hi.baidu.com/haigang/blog/item/e5f582262d6 ...

  6. 多种方式判断字符串是否为空,效率比拼

    一.情景 判断输入字符串是否为空   分析:null   && "" || length()==0 二.区分null 与"" null:字符串不 ...

  7. unistd.h 中int access(const char * pathname, int mode); 判断进程能否以mode模式访问pathname文件(可以用来判断文件/目录是否存在)...

    mode 可以使 R_OK,W_OK,X_OK(是否可执行),F_OK(文件是否存在)的掩码. 成功返回0,失败返回-1 转载于:https://www.cnblogs.com/lc-cnblong/ ...

  8. C++ WINDOWS API 第2章 Windows API概要

    目录 2.1       Windows数据类型.. 1 2.1.1       Windows数据类型示例.. 1 2.1.2       Windows 数据类型与标准C 数据类型的关系.. 5 ...

  9. linux php目录是否存在,PHP判断文件或者目录是否可写,兼容windows/linux系统

    在PHP中,可用is_writable()函数来判断一个 文件/目录 是否可写,用是否可生成文件的方式判断目录是否可写:网上的一些代码大多数能判断linux系统,但windows服务器下判断不准确. ...

最新文章

  1. jsPlumb(3)-基本概念
  2. nvm 下载node 下载不下来_一键下载网页所有图片,把美丽存下来
  3. window.requestAnimationFrame
  4. 这款Linux发行版已经适配200+移动设备
  5. 如何在ubuntu下安装detectron2_Ubuntu下detectron2 的安装使用笔记
  6. 【常用】2DUI跟随3D物体移动(待修复)
  7. SpringMVC开发框架中使用@ResponseBody注解后返回的json字符串中文乱码问题
  8. jeecms v3.x标签教程之[@cms_comment_page]
  9. python文件中执行py文件
  10. python实现app自动签到器_python实现网页自动签到功能
  11. 电脑只能上微信不能打开网页_怎么回事?我的电脑浏览器打不开网页,但微信、QQ却又能用?...
  12. html页面栅格系统,超好用的网页栅格化工具: GridGuide
  13. Lm317电压源芯片制作电流源
  14. 客户案例 | 高等教育出版社
  15. 李航老师新作《机器学习方法》上市了!附购买链接
  16. 给代码写注释时有哪些讲究?
  17. arm-gcc链接器和链接脚本
  18. 用pytest.fixture处理接口自动化跨文件token传参
  19. Android zxing,轻松实现二维码扫描、生成
  20. 中国网建提供的SMS短信发送

热门文章

  1. mysql 回档_数据库回档解决方案
  2. 挫折是上天给我们的赏赐
  3. js修改css hover样式,JS实现css hover操作的方法示例
  4. window40系统怎么重装不下服务器,Win10系统异常不想重装怎么办 四种系统修复方法哪种比较好...
  5. python坐标轴位置_Python Matplotlib 改变坐标轴的默认位置
  6. 近年来高层建筑火灾频发,这些安全逃生知识要知道
  7. java sql语句逗号_Java 实现对Sql语句解析
  8. CSMA/CA协议详解【计算机网络】
  9. 无人驾驶汽车入门_无人驾驶汽车将如何扭转10,000年的趋势
  10. SMARTFORM 插入列、增加表格边框和底纹