判断文件、目录是否存在:C、C++、Windows API、 boost

本文详细介绍了在Windows和Linux环境下使用多种方法判断文件和目录是否存在的技术,包括C/C++标准库函数、Windows API、Boost库等,并对比了各种方法的适用场景。

一、判断文件是否存在

#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 linux
    if ( 0 == access(file_name, 0) )  cout<<"access(): file exist."<<endl;
    else                              cout<<"access(): file not exist."<<endl;
    
 
    //C++     run in windows and linux
    fstream 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 WIN32
    WIN32_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 linux
    boost::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 linux
    if ( 0 == access(file_name, 0) )  cout<<"access(): path exist."<<endl;
    else                              cout<<"access(): path not exist."<<endl;
 
 
    //Windows API     run in windows
#ifdef WIN32
    WIN32_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 linux
    boost::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)
--------------------- 
作者:kanguolaikanguolaik 
来源:优快云 
原文:https://blog.youkuaiyun.com/guowenyan001/article/details/17259173 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值