您的位置:首页 > 其它

检测一个文件是否存在的方法

2015-12-24 10:29 731 查看
这个总结是参考了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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: