diff --git a/include/storage/io.hpp b/include/storage/io.hpp index dbd6330ab..7a7b6f536 100644 --- a/include/storage/io.hpp +++ b/include/storage/io.hpp @@ -56,21 +56,17 @@ class FileReader "bytewise reading requires trivially copyable type"); if (count == 0) return; - input_stream.read(reinterpret_cast(dest), count * sizeof(T)); - // safe to cast here, according to CPP docs, negative values for gcount - // are never used. - const unsigned long bytes_read = static_cast(input_stream.gcount()); - const auto expected_bytes = count * sizeof(T); - - if (bytes_read == 0) + const auto &result = input_stream.read(reinterpret_cast(dest), count * sizeof(T)); + if (!result) { + if (result.eof()) + { + throw util::exception("Error reading from " + filename + + ": Unexpected end of file"); + } throw util::exception("Error reading from " + filename + ": " + std::strerror(errno)); } - else if (bytes_read < expected_bytes) - { - throw util::exception("Error reading from " + filename + ": Unexpected end of file"); - } } template void ReadInto(T &target) { ReadInto(&target, 1); }