为了在我的代码中读取文件,我使用 std::filesystem::file_size ( https://en.cppreference.com/w/cpp/filesystem/file_size ) 来获取他的大小。我使用这段代码:
template <typename TYPE>
inline void read_binary_file(const fs::path filename, std::vector<TYPE>& result)
{
std::ifstream file(filename, std::ios::in | std::ios::binary);
__ERR_READFILE01__ // file cannot be open
SPDLOG_DEBUG("Reading {}", filename.u8string());
size_t filesize;
try
{
filesize = fs::file_size(filename);
}
catch(fs::filesystem_error& e)
{
std::cout << e.what() << '\n'; abort();
}
assert(filesize%sizeof(TYPE) == 0);
SPDLOG_DEBUG("size of file {}", filesize);
SPDLOG_DEBUG("size of {}", static_cast<std::uintmax_t>(-1));
SPDLOG_DEBUG("size of type {}", sizeof(TYPE));
SPDLOG_DEBUG("size of the reading vector {}", filesize/sizeof(TYPE));
result.resize(filesize/sizeof(TYPE));
file.read(reinterpret_cast<char*>(result.data()), filesize);
file.close();
}
这适用于我需要阅读的大多数文件,但是对于文件(〜3 gigas),我有一个奇怪的问题:
[07/12/2020 11:52:42][debug] size of file 18446744072617361848
[07/12/2020 11:52:42][debug] size of 18446744073709551615
[07/12/2020 11:52:42][debug] size of type 4
[07/12/2020 11:52:42][debug] size of the reading vector 4611686018154340462
在文档中,我可以阅读The non-throwing overload returns static_cast<std::uintmax_t>(-1) on errors.
. 但是值 18446744072617361848 与 不同static_cast<std::uintmax_t>(-1)
,所以我迷路了....
我的编译器是 mingw32 :
mingw32-make.exe --version
GNU Make 4.3 Built for Windows32 Copyright (C) 1988-2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
我在 linux (gcc) 上没有这个问题。