c++ 判断文件是否存在的几种方法
一般方法
一般而言,下述方法都可以检查文件是否存在:
- 使用ifstream打开文件流,成功则存在,失败则不存在
- 以fopen读方式打开文件,成功则存在,否则不存在
- 使用access函数获取文件状态,成功则存在,否则不存在
- 使用stat函数获取文件状态,成功则存在,否则不存在
代码如下:
#include
#include
#include
#include inline bool exists_test0 (const std::string& name) {ifstream f(name.c_str());return f.good();
}inline bool exists_test1 (const std::string& name) {if (FILE *file = fopen(name.c_str(), "r")) {fclose(file);return true;} else {return false;}
}inline bool exists_test2 (const std::string& name) {return ( access( name.c_str(), F_OK ) != -1 );
}inline bool exists_test3 (const std::string& name) {struct stat buffer; return (stat (name.c_str(), &buffer) == 0);
}
参考资料中有性能测试对比,结果表明,使用 stat() 函数的方式性能最好。
# Results for total time to run the 100,000 calls averaged over 5 runs,Method exists_test0 (ifstream): **0.485s**
Method exists_test1 (FILE fopen): **0.302s**
Method exists_test2 (posix access()): **0.202s**
Method exists_test3 (posix stat()): **0.134s**
boost库
boost.filesystem在发生错误的时候会抛出异常,但是在大部分情况下这些异常是可以忽略的,例如,在检查文件是否存在的时候,发生错误可以等同于文件不存在。
虽然boost.filesystem也提供了重载函数,通过输出参数返回错误来代替异常,但是在每个调用点都得定义一个输出参数,稍显麻烦。
所以,为了简化客户代码,我们实现了一些包装函数,如下所示:
bool IsFileExistent(const boost::filesystem::path& path) {boost::system:error_code error;return boost::filesystem::is_regular_file(path, error);
}
上面的函数用来检查文件是否存在,使用了boost::filesystem::is_regular_file。当path指向一个“常规文件”的时候,认为该文件存在;否则其它任何情况都认为文件不存在。
对于只有常规文件的情况,该函数没有问题。但是,如果还存在其他文件时,如符号链接文件时,则返回文件不存在。
事实上,用boost::filesystem::status获取时,会返回symlink_file,boost.filesystem将它们视为符号链接文件。
不论是常规文件还是符号链接文件,呈现给用户的都是能够正常使用的文件。
所以,不能单纯地用boost::filesystem::is_regular_file来检查文件是否存在了,下面是包装函数的改进版本:
bool IsFileExistent(const boost::filesystem::path& path) {boost::system:error_code error;auto file_status = boost::filesystem::status(path, error);if (error) {return false;}if (! boost::filesystem::exists(file_status)) {return false;}if (boost::filesystem::is_directory(file_status)) {return false;}return true;
}
首先,通过boost::filesystem::status获取文件的信息,如果发生错误,则认为文件不存在。
然后,使用boost::filesystem::exists判断文件是否存在,该函数不区分文件夹和文件,所以最后还要使用boost::filesystem::is_directory判断一下是否文件夹,只要不是文件夹,都认为文件是存在的。
参考资料:
Fastest way to check if a file exist using standard C++/C++11/C?
使用boost.filesystem检查文件是否存在的正确方式
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
