C/C++ 判断文件是否存在
代码
#include <sys/stat.h>
#include <unistd.h>
#include <string>
#include <fstream>
inline bool exists_ifstream (const std::string& name) {
std::ifstream fin(name.c_str());
return f.good();
}
inline bool exists_fopen (const std::string& name) {
if (FILE *file = fopen(name.c_str(), "r")) {
fclose(file);
return true;
}
return false;
}
inline bool exists_access (const std::string& name) {
return ( access( name.c_str(), F_OK ) != -1 );
}
inline bool exists_stat (const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
在参考资料的评测中,stat 的性能最好。
Reference
- c++ 判断文件是否存在的几种方法
- Fastest way to check if a file exists using standard C++/C++11,14,17/C?
最后
以上就是暴躁仙人掌最近收集整理的关于C/C++ 判断文件是否存在的全部内容,更多相关C/C++内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复