这个总结是参考了Stackoverflow的一遍文章http://stackoverflow.com/questions/12774207/fastest-way-to-check-if-a-file-exist-using-standard-c-c11-c,
方法如下
方法一:利用ifstream
bool exists(std::string file)
{
std::ifstreamfile(file);
boolstate = file.good();
file.close();
returnstate;
}
方法二:还是利用ifstream
bool exists(std::string file)
{
std::ifstreamfile(file);
if(file){
file.close();
returntrue;
}else{
file.close();
returnfalse;
}
}
方法三,利用Windows的API,PathFileExists
BOOL exists(LPCTSTR pszPath)
{
returnPathFileExists(pszPath);
}
方法四,利用Windows的API,GetFileAttributes
BOOL exists(LPCTSTR pszPath)
{
// Theattributes of the specified file or directory, returned in a DWORD,
//indicates success.0xFFFFFFFF indicates failure.
return(GetFileAttributes (pszPath) != 0xFFFFFFFF);
}