1. 程式人生 > >判斷檔案、目錄是否存在:C、C++、Windows API、 boost

判斷檔案、目錄是否存在:C、C++、Windows API、 boost

一、判斷檔案是否存在

  1. #ifdef WIN32
  2. #include <io.h>                      //C (Windows)    access
  3. #else
  4. #include <unistd.h>                  //C (Linux)      access   
  5. #endif
  6. #include <fstream>                   //C++           fstream
  7. #ifdef WIN32
  8. #include <Windows.h>                 //Windows API   FindFirstFile
  9. #include <Shlwapi.h>
  10. #pragma comment(lib, "shlwapi.lib")  //Windows API   PathFileExists
  11. #endif
  12. #include <boost/filesystem.hpp>      //boost         
  13. usingnamespace std;  
  14. int main()  
  15. {  
  16.     char file_name[] = "D://aa.txt";  
  17.     //C     run in windows and linux
  18.     if ( 0 == access(file_name, 0) )  cout<<
    "access(): file exist."<<endl;  
  19.     else                              cout<<"access(): file not exist."<<endl;  
  20.     //C++     run in windows and linux
  21.     fstream fs;  
  22.     fs.open(file_name, ios::in);  
  23.     if (fs)   cout<<"fstream: file exist."<<endl;  
  24.     else      cout<<
    "fstream: file not exist."<<endl;  
  25.     fs.close();  
  26.     //Windows API     run in windows
  27. #ifdef WIN32
  28.     WIN32_FIND_DATA wfd;  
  29.     HANDLE hFind = FindFirstFile(file_name, &wfd);  
  30.     if ( INVALID_HANDLE_VALUE != hFind )   cout<<"FindFirstFile: file exist."<<endl;  
  31.     else                                   cout<<"FindFirstFile: file not exist."<<endl;   
  32.     CloseHandle(hFind);  
  33.     if ( PathFileExists(file_name) )       cout<<"PathFileExists: file exist."<<endl;  
  34.     else                                   cout<<"PathFileExists: file not exist."<<endl;  
  35.     if ( INVALID_FILE_ATTRIBUTES != GetFileAttributes(file_name) )   cout<<"GetFileAttributes: file exist."<<endl;  
  36.     else                                                             cout<<"GetFileAttributes: file not exist."<<endl;  
  37.     if ( INVALID_HANDLE_VALUE != CreateFile(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, NULL, NULL) )  cout<<"CreateFile: file exist."<<endl;  
  38.     else                                                                                                    cout<<"CreateFile: file not exist."<<endl;  
  39. #endif
  40.     //boost      run in windows and linux
  41.     boost::filesystem::path path_file(file_name);  
  42.     if ( boost::filesystem::exists(path_file) &&   
  43.          boost::filesystem::is_regular_file(path_file) )   cout<<"boost: file exist."<<endl;  
  44.     else                                                   cout<<"boost: file not exist."<<endl;  
  45.     return 0;  
  46. }  

二、判斷目錄是否存在

  1. #ifdef WIN32
  2. #include <io.h>                      //C (Windows)    access
  3. #else
  4. #include <unistd.h>                  //C (Linux)      access   
  5. #endif
  6. #ifdef WIN32
  7. #include <Windows.h>                 //Windows API   FindFirstFile
  8. #include <Shlwapi.h>
  9. #pragma comment(lib, "shlwapi.lib")  //Windows API   PathFileExists
  10. #endif
  11. #include <boost/filesystem.hpp>      //boost         
  12. usingnamespace std;  
  13. int main()  
  14. {  
  15.     char file_name[] = "D://b";  
  16.     //C     run in windows and linux
  17.     if ( 0 == access(file_name, 0) )  cout<<"access(): path exist."<<endl;  
  18.     else                              cout<<"access(): path not exist."<<endl;  
  19.     //Windows API     run in windows
  20. #ifdef WIN32
  21.     WIN32_FIND_DATA wfd;  
  22.     HANDLE hFind = FindFirstFile(file_name, &wfd);  
  23.     if ( INVALID_HANDLE_VALUE != hFind && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )   cout<<"FindFirstFile: path exist."<<endl;  
  24.     else                                                                                        cout<<"FindFirstFile: path not exist."<<endl;   
  25.     CloseHandle(hFind);  
  26.     if ( PathFileExists(file_name) )       cout<<"PathFileExists: path exist."<<endl;  
  27.     else                                   cout<<"PathFileExists: path not exist."<<endl;  
  28.     if ( INVALID_FILE_ATTRIBUTES != GetFileAttributes(file_name) )   cout<<"GetFileAttributes: path exist."<<endl;  
  29.     else                                                             cout<<"GetFileAttributes: path not exist."<<endl;     
  30. #endif
  31.     //boost      run in windows and linux
  32.     boost::filesystem::path path_file(file_name);  
  33.     if ( boost::filesystem::exists(path_file) &&   
  34.          boost::filesystem::is_directory(path_file) )      cout<<"boost: path exist."<<endl;  
  35.     else                                                   cout<<"boost: path not exist."<<endl;  
  36.     return 0;  
  37. }  

三、幾種方式比較

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)

轉自:http://blog.csdn.net/guowenyan001/article/details/17259173