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());
}
4

1 回答 1

4

使用不抛出的重载。

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

但是,您将需要检查输出参数,因此这可能比捕获异常更有效。这取决于您要完成的工作。

于 2013-03-10T17:08:39.930 回答