欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-09 23:50:24
...

一、判断文件是否存在

#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 
来源:CSDN 
原文:https://blog.csdn.net/guowenyan001/article/details/17259173 
版权声明:本文为博主原创文章,转载请附上博文链接!

相关标签: 路径 文件