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

一、判断文件是否存在

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #ifdef WIN32  
  2. #include <io.h>                      //C (Windows)    access  
  3. #else  
  4. #include <unistd.h>                  //C (Linux)      access     
  5. #endif  
  6.   
  7. #include <fstream>                   //C++           fstream  
  8.   
  9. #ifdef WIN32  
  10. #include <Windows.h>                 //Windows API   FindFirstFile  
  11. #include <Shlwapi.h>  
  12. #pragma comment(lib, "shlwapi.lib")  //Windows API   PathFileExists  
  13. #endif  
  14.   
  15. #include <boost/filesystem.hpp>      //boost           
  16. using namespace std;  
  17.   
  18.   
  19.   
  20.   
  21. int main()  
  22. {  
  23.     char file_name[] = "D://aa.txt";  
  24.   
  25.   
  26.     //C     run in windows and linux  
  27.     if ( 0 == access(file_name, 0) )  cout<<"access(): file exist."<<endl;  
  28.     else                              cout<<"access(): file not exist."<<endl;  
  29.       
  30.   
  31.     //C++     run in windows and linux  
  32.     fstream fs;  
  33.     fs.open(file_name, ios::in);  
  34.     if (fs)   cout<<"fstream: file exist."<<endl;  
  35.     else      cout<<"fstream: file not exist."<<endl;  
  36.     fs.close();  
  37.   
  38.   
  39.     //Windows API     run in windows  
  40. #ifdef WIN32  
  41.     WIN32_FIND_DATA wfd;  
  42.     HANDLE hFind = FindFirstFile(file_name, &wfd);  
  43.     if ( INVALID_HANDLE_VALUE != hFind )   cout<<"FindFirstFile: file exist."<<endl;  
  44.     else                                   cout<<"FindFirstFile: file not exist."<<endl;   
  45.     CloseHandle(hFind);  
  46.   
  47.     if ( PathFileExists(file_name) )       cout<<"PathFileExists: file exist."<<endl;  
  48.     else                                   cout<<"PathFileExists: file not exist."<<endl;  
  49.   
  50.     if ( INVALID_FILE_ATTRIBUTES != GetFileAttributes(file_name) )   cout<<"GetFileAttributes: file exist."<<endl;  
  51.     else                                                             cout<<"GetFileAttributes: file not exist."<<endl;  
  52.   
  53.     if ( INVALID_HANDLE_VALUE != CreateFile(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, NULL, NULL) )  cout<<"CreateFile: file exist."<<endl;  
  54.     else                                                                                                    cout<<"CreateFile: file not exist."<<endl;  
  55. #endif  
  56.   
  57.   
  58.     //boost      run in windows and linux  
  59.     boost::filesystem::path path_file(file_name);  
  60.     if ( boost::filesystem::exists(path_file) &&   
  61.          boost::filesystem::is_regular_file(path_file) )   cout<<"boost: file exist."<<endl;  
  62.     else                                                   cout<<"boost: file not exist."<<endl;  
  63.       
  64.   
  65.     return 0;  
  66. }  

二、判断目录是否存在

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #ifdef WIN32  
  2. #include <io.h>                      //C (Windows)    access  
  3. #else  
  4. #include <unistd.h>                  //C (Linux)      access     
  5. #endif  
  6.   
  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.   
  13. #include <boost/filesystem.hpp>      //boost           
  14. using namespace std;  
  15.   
  16.   
  17.   
  18.   
  19. int main()  
  20. {  
  21.     char file_name[] = "D://b";  
  22.   
  23.   
  24.     //C     run in windows and linux  
  25.     if ( 0 == access(file_name, 0) )  cout<<"access(): path exist."<<endl;  
  26.     else                              cout<<"access(): path not exist."<<endl;  
  27.   
  28.   
  29.     //Windows API     run in windows  
  30. #ifdef WIN32  
  31.     WIN32_FIND_DATA wfd;  
  32.     HANDLE hFind = FindFirstFile(file_name, &wfd);  
  33.     if ( INVALID_HANDLE_VALUE != hFind && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )   cout<<"FindFirstFile: path exist."<<endl;  
  34.     else                                                                                        cout<<"FindFirstFile: path not exist."<<endl;   
  35.     CloseHandle(hFind);  
  36.   
  37.     if ( PathFileExists(file_name) )       cout<<"PathFileExists: path exist."<<endl;  
  38.     else                                   cout<<"PathFileExists: path not exist."<<endl;  
  39.   
  40.     if ( INVALID_FILE_ATTRIBUTES != GetFileAttributes(file_name) )   cout<<"GetFileAttributes: path exist."<<endl;  
  41.     else                                                             cout<<"GetFileAttributes: path not exist."<<endl;     
  42. #endif  
  43.   
  44.   
  45.     //boost      run in windows and linux  
  46.     boost::filesystem::path path_file(file_name);  
  47.     if ( boost::filesystem::exists(path_file) &&   
  48.          boost::filesystem::is_directory(path_file) )      cout<<"boost: path exist."<<endl;  
  49.     else                                                   cout<<"boost: path not exist."<<endl;  
  50.       
  51.   
  52.     return 0;  
  53. }  

三、几种方式比较

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值