判断路径(文件或文件夹)是否存在[cpp]view plain copy print ?BOOLFileExist(CStringstrPath) { WIN32_FIND_DATAwfd; BOOLrValue=FALSE; HANDLEhFind=FindFirstFile(strPath,&wfd); if((hFind!=INVALID_HANDLE_VALUE) &&(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) { rValue=TRUE; } FindClose(hFind); returnrValue; }
判断路径是否为文件夹
[cpp]view plain copy print ?#pragmacomment(lib,"shlwapi.lib") #include<shlwapi.h> BOOLPathIsDirectory(strPath)
判断文件是否存在。
1._access函数,在io.h中。
原型:int _access(const char *filename, int amode);
参数amode(好象有5种模式)
0:检查文件是否存在
1:检查文件是否可运行
2:检查文件是否可写访问
4:检查文件是否可读访问
还有一种,由于MSDN突然坏了,暂时保留着
[cpp]view plain copy print ?if(_access(file,0)) { //文件不存在 }
2.CFile和CFileStatus类
CFile的静态函数GetStatus如果返回FALSE表示文件不存在
[cpp]view plain copy print ?CFileStatusfs; if(!CFile::GetStatus(strFileName,fs)) { //文件不存在 }
3.CFileFind类
直接使用该类的成员函数FindFile进行判断
[cpp]view plain copy print ?CFileFindff; if(!ff.FindFile(strFileName)) { //文件不存在 } ff.Close();
3.判断文件夹是否存在
[cpp]view plain copy print ?DirExists(sPath);
转自:/wangjieest/article/details/7000640
如果觉得《判断文件是否存在 判断是否为文件夹》对你有帮助,请点赞、收藏,并留下你的观点哦!