在当今的 C++ 中,构建 fstream 时我能可靠地得到错误吗?

问题描述 投票:0回答:1

我想使用 C++ 标准库工具 (

std::ifstream
) 读取文件 - 当然,如果遇到错误,可以可靠地报告错误。

显然,这并不是一件容易的事!

  • std::basic_fstream
    std::ifstream
    是其实例化的模板)默认情况下不会抛出异常。
  • 您可以使基本的 fstream 抛出异常 - 但只能在构造之后,因此构造不会失败。请参阅
    basic_ios::exceptions()
    (这是
    std::ifstream
    的超超类)。

14年前,有人问过这个问题:

获取 std::fstream 失败错误消息和/或异常

答案告诉我们:

  1. 不能保证抛出的异常会告诉我们错误的原因是什么(只是发生了一些错误)
  2. 我们不能保证在 fstream 上设置失败位或坏位时,
    errno
    /
    GetLastError()
    为我们提供非零/非成功值。

这很糟糕。另一方面,14年过去了。有什么改变吗?也就是说,对于抛出的异常或设置

errno
/
GetLastError()
是否有更好的保证?如果不是,在构建
std::fstream
时报告错误的“尽力而为”方法是什么?

(我很想问“为什么构造函数不会在失败时抛出异常,但我们不讨论这个。)

c++ error-handling fstream ifstream system-error
1个回答
1
投票

这是我现在能想到的最好的事情 - “捂住我的屁股”,以防

errno
不知何故未设置。最坏的情况是我浪费了一些周期重新投入“不愉快的道路”。

// TODO: Consider checking errno here
std::filesystem::path file_path = whatever();
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
try {
    file.exceptions(std::ios::failbit | std::ios::badbit);
} catch (std::ios_base::failure& exception) {
    if (errno == 0) {
        throw;
    }
    throw std::system_error{ errno, std::generic_category(),
        "opening file " + file_path.native());
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.