gpt4 book ai didi

C++ ofstream输出到图像文件在Windows上写入不同的数据

转载 作者:太空狗 更新时间:2023-10-29 20:26:55 25 4
gpt4 key购买 nike

我在做一件简单的事情:将存储为字符串的图像文件的数据写入包含该字符串的图像文件。

std::ofstream f("image.jpeg");
f << image_data; // image_data was created using python and copied over, in hex and turned back into ascii

然而,意想不到的事情发生了:

Good Picture

变成:

Bad Picture

我不明白为什么会这样。

When I use python2.7 to get the data from the original picture and write it to a new file, it works fine.

When I compile and run my program in ubuntu, the picture comes out fine.

When I write a large text file (larger than the image) into a .txt, the file comes out fine.

It is only jpegs on Windows that fails. The original image I tried was an image from a PGP key packet, which came out with half of the person's head clear and the other half messed up.

编译后的程序并没有弄乱所有的数据,因为就像我上面说的,一些原始图片被显示出来了。此外,图片大小相同,因此至少保留了 jpeg 格式。

发生了什么事?我在 Windows 7 上的 Code::Blocks 中使用 ming2 4.7.2。Windows 是不是疯了?

最佳答案

您必须以二进制模式打开文件:

std::ofstream f("image.jpeg", std::ios::out | std::ios::binary);
// ^^^^^^^^^^^^^^^^

关于C++ ofstream输出到图像文件在Windows上写入不同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17826488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com