boost::filesystem::exists崩溃

3
我使用boost 1.52版本,当我尝试从未授权读取的网络驱动器中获取文件时,我会遇到异常,在使用boost::filesystem::exists(fileName)后。
有没有比在每个地方都使用try, catch更好的解决方法?

目前我已经切换回我的旧代码:

bool FileExists(const char* fileName)
{
    struct stat my_stat;
    return (stat(fileName, &my_stat) == 0);
}

//boost Exists throws exception if there are no permissions for share folder
bool FileExists(const std::string& fileName)
{
    return FileExists(fileName.c_str());
}

1
你能不能只使用现在正在使用的函数,将 stat 替换为 boost::filesystem::exists 并加上必要的异常处理呢?这样你就不必在每个地方都使用 try-catch 了。它只在那个函数中被隔离了。 - Benjamin Lindley
1个回答

4

使用不会抛出异常的重载函数

bool exists(const path& p, system::error_code& ec) noexcept;

您可能需要检查输出参数,但这可能比捕获异常更费力。 这取决于您想要实现什么。


我只是想获取一个配置文件。在开始解析之前,我想检查它是否存在,以及我是否有权限从网络驱动器上读取它。 - Gilad
你是否意识到这个想法中固有的竞态条件?文件可能在 exists 返回后但在解析之前被删除。 - Sam Miller
是的,然而这是一个简单的系统,用户已经意识到了这个问题。我想在这里提出的另一个问题是权限。 - Gilad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接