C++ 在windows上读取和写入TGA文件,产生不正确的输出

C++ 在windows上读取和写入TGA文件,产生不正确的输出,c++,windows,ifstream,ofstream,tga,C++,Windows,Ifstream,Ofstream,Tga,此基本示例使用相同的TGA文件在macOS上工作,但由于某些原因,在使用此特定文件的windows上,输出如所提供的图像所示 std::ifstream input_file("C:\\Users\\Michael\\Desktop\\earth.tga", std::ios::binary); input_file.seekg(0, std::ios::end); std::size_t length = input_file.tellg(); input_file.se

此基本示例使用相同的TGA文件在macOS上工作,但由于某些原因,在使用此特定文件的windows上,输出如所提供的图像所示

std::ifstream input_file("C:\\Users\\Michael\\Desktop\\earth.tga", std::ios::binary);

input_file.seekg(0, std::ios::end);
std::size_t length = input_file.tellg();
input_file.seekg(0, std::ios::beg);
std::vector<char> data(length);
input_file.read(data.data(), length);

std::ofstream output_file("C:\\Users\\Michael\\Desktop\\output.tga", std::ofstream::out);

output_file.write(data.data(), data.size());
std::ifstream输入文件(“C:\\Users\\Michael\\Desktop\\earth.tga”,std::ios::binary);
输入_file.seekg(0,std::ios::end);
std::size_t length=input_file.tellg();
输入_file.seekg(0,std::ios::beg);
std::矢量数据(长度);
输入_file.read(data.data(),长度);
std::ofstream output_文件(“C:\\Users\\Michael\\Desktop\\output.tga”,std::ofstream::out);
output_file.write(data.data(),data.size());
earth.tga:

output.tga:


我试过的其他tga文件工作正常。windows上的ifstream有什么不同可能导致此问题?您可以在此处找到tga

您的输出流似乎不是binary.FYI——您应该在十六进制编辑器中加载好的输出文件和坏的输出文件,并观察它们之间的差异。如果你这样做了,你可能会注意到无关的字符。你不能仅仅通过查看图片来调试图像问题。