跨平台判断是文件还是文件夹

74 篇文章 1 订阅
19 篇文章 4 订阅

跨平台判断是文件还是文件夹

分类: Windows Linux C++   414人阅读  评论(0)  收藏  举报

判断是文件还是文件夹。

[cpp]  view plain copy
  1. #ifdef WIN32  
  2. #include <windows.h>  
  3. #include <direct.h>  
  4. #else  
  5. #include <unistd.h>  
  6. #include <sys/types.h>  
  7. #include <sys/stat.h>  
  8. #include <dirent.h>  
  9. #endif  
  10.   
  11. /**    
  12. * @brief isFileOrDir    
  13. *    
  14. * Detailed description.   
  15. * @param[in] path    
  16. * @return int 0:不存在,1:为文件夹, -1:为文件   
  17. */    
  18. inline int isFileOrDir(std::string path)     
  19. {     
  20. #ifdef WIN32     
  21.     DWORD dwAttr = GetFileAttributes(path.c_str());      
  22.     //Not exist     
  23.     if (dwAttr == 0xFFFFFFFF)     
  24.     {     
  25.         return 0;     
  26.     }     
  27.     //exist     
  28.     else if (dwAttr & FILE_ATTRIBUTE_DIRECTORY)     
  29.     {     
  30.         return 1;     
  31.     }     
  32.     //is File     
  33.     else    
  34.     {     
  35.         return -1;     
  36.     }     
  37. #else     
  38.     if (0 == access(path.c_str(), 0))     
  39.     {     
  40.         struct stat *buf;     
  41.   
  42.         buf = (struct stat *) malloc(sizeof(struct stat));     
  43.         memset(buf, 0, sizeof(struct stat));     
  44.   
  45.         stat(path.c_str(), buf);     
  46.   
  47.         if (S_ISDIR(buf->st_mode))     
  48.         {     
  49.             free(buf);     
  50.             buf=NULL;     
  51.             return 1;     
  52.         }     
  53.         else         
  54.         {     
  55.             free(buf);     
  56.             buf=NULL;     
  57.             return -1;     
  58.         }     
  59.     }     
  60.     else    
  61.     {     
  62.         return 0;     
  63.     }     
  64. #endif     
  65. }    

 

关键词:文件 文件夹 判断 是否为文件 是否为文件夹 文件是否存在 文件夹是否存在 跨平台 Linux Windows
Key Words: File, Directory, Exists, Cross-platform, Linux, Windows
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值