我正在将 C++ 代码从 Linux 移植到 Windows。我目前正在使用 Visual Studio 2013 来移植我的代码。
我需要读取二进制文件并使用这部分 C++ 代码:
// Open the stream
std::ifstream is("myfile.bin");
// Determine the file length
is.seekg(0, std::ios_base::end);
std::size_t size=is.tellg();
is.seekg(0, std::ios_base::begin);
// Create a vector to store the data
int* Data = new int[size/sizeof(int)];
// Load the data
is.read((char*) &Data[0], size);
// Close the file
is.close();
在 linux 中,我的二进制文件的大小被正确地发现为 744mb。但是,在 Windows 中,我的二进制文件的大小被错误地发现为 >4GB。我该如何纠正这个问题?
最佳答案
将 std::ifstream is("myfile.bin");
更改为 std::ifstream is("myfile.bin", std::ios::binary);
对于您当前的默认打开模式,编译器选择“char”模式。在 Linux 中,文件中的字符是 UTF8,前 128 个位置是 1 字节字符。但是对于 UTF32 内存,每个字符使用 4 个字节。在 Windows 中,字符是“宽字符”,每个字符 2 个字节。
关于c++ - 使用 Visual Studio C++ 找到的文件大小不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41488971/