判断文件是否存在,判断是否为文件夹

来源:互联网 发布:淘宝模特韩国短发 编辑:程序博客网 时间:2024/06/06 03:02
判断路径(文件或文件夹)是否存在
[cpp] view plaincopyprint?
  1. BOOL FileExist(CString strPath)  
  2. {  
  3.     WIN32_FIND_DATA wfd;  
  4.     BOOL rValue = FALSE;  
  5.     HANDLE hFind = FindFirstFile(strPath, &wfd);  
  6.     if ((hFind!=INVALID_HANDLE_VALUE)   
  7.         &&(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))  
  8.     {  
  9.         rValue = TRUE;  
  10.     }  
  11.     FindClose(hFind);  
  12.     return rValue;  
  13. }  

判断路径是否为文件夹

[cpp] view plaincopyprint?
  1. #pragma comment(lib,"shlwapi.lib")  
  2. #include <shlwapi.h>  
  3.   
  4. BOOL PathIsDirectory(strPath)  


判断文件是否存在。

  1._access函数,在io.h中。

  原型:int _access(const char *filename, int amode);

  参数amode(好象有5种模式)

  0:检查文件是否存在

  1:检查文件是否可运行

  2:检查文件是否可写访问

  4:检查文件是否可读访问

  还有一种,由于MSDN突然坏了,暂时保留着

[cpp] view plaincopyprint?
  1. if ( _access(file,0) )  
  2.   
  3. {  
  4.   
  5. //文件不存在  
  6.   
  7. }  

  2.CFile和CFileStatus类

  CFile的静态函数GetStatus如果返回FALSE表示文件不存在

[cpp] view plaincopyprint?
  1. CFileStatus fs;  
  2.   
  3. if ( !CFile::GetStatus(strFileName,fs) )  
  4.   
  5. {  
  6.   
  7. //文件不存在  
  8.   
  9. }  

  3.CFileFind类

  直接使用该类的成员函数FindFile进行判断

[cpp] view plaincopyprint?
  1. CFileFind ff;  
  2.   
  3. if ( !ff.FindFile(strFileName) )  
  4.   
  5. {  
  6.   
  7. //文件不存在  
  8.   
  9. }  
  10.   
  11. ff.Close();  

  3.判断文件夹是否存在

[cpp] view plaincopyprint?
  1. DirExists(sPath);  


转自:http://blog.csdn.net/wangjieest/article/details/7000640

原创粉丝点击