c++ 判断文件存在方法

一般方法

一般而言,下述方法都可以检查文件是否存在:

  • 使用ifstream打开文件流,成功则存在,失败则不存在
  • 以fopen读方式打开文件,成功则存在,否则不存在
  • 使用access函数获取文件状态,成功则存在,否则不存在
  • 使用stat函数获取文件状态,成功则存在,否则不存在

代码如下:

#include <sys/stat.h>
#include <unistd.h>
#include <string>
#include <fstream>

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); 
}
123456789101112131415161718192021222324252627

参考资料中有性能测试对比,结果表明,使用 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**
123456
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);
}
1234

上面的函数用来检查文件是否存在,使用了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;
}
123456789101112131415161718

首先,通过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检查文件是否存在的正确方式

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值