C++(Visual Studio),不能写数字'10'要归档,所有其他号码都有效吗? [英] C++ (Visual Studio), Can't write the number '10' to file, all other numbers working?
问题描述
我这里有一个奇怪的问题!我正在尝试为 8 位 windows 3.x 位图文件编写颜色表.我只希望文件是灰度的,所以我想写 bbb0、ggg0、rrr0 256 次,其中 r=g=b=1..256
I have a bit of a weird problem here! I am trying to write the color table for a 8 bit windows 3.x bitmap file. I just want the file to be greyscale, so i am trying to write bbb0, ggg0, rrr0 256 times where r=g=b=1..256
//write greyscale color table
for (int i = 255; i >= 0; i--) {
writeS = (unsigned short)i;
outfile.write ((char*)&writeS,sizeof(char)); // b
outfile.write ((char*)&writeS,sizeof(char)); // g
outfile.write ((char*)&writeS,sizeof(char)); // r
writeS = 0;
outfile.write ((char*)&writeS,sizeof(char)); // 0
}
当我使用十六进制编辑器查看输出时,一切看起来都很好,直到我看到数字 10,它是这样写的:
When I look at the output i am getting with a hex editor, everything looks fine until I get to the number 10, which is written like so:
...0C 0C 0C 00 0B 0B 0B 00 0D 0A 0D 0A 0D 0A 00 09 09 09 00 08 08 08 00...
...0C 0C 0C 00 0B 0B 0B 00 0D 0A 0D 0A 0D 0A 00 09 09 09 00 08 08 08 00...
不是:
...0C 0C 0C 00 0B 0B 0B 00 0A 0A 0A 00 09 09 09 00 08 08 08 00...
...0C 0C 0C 00 0B 0B 0B 00 0A 0A 0A 00 09 09 09 00 08 08 08 00...
奇怪的是,它只对这个数字执行此操作,但更奇怪的是,当我更改代码以跳过数字 10 并改为写入 9 时,它可以工作.
So that is wierd that it is only doing it on this one number, but what is even wierder is that when I change the code to skip the number 10 and write 9 instead, it works.
//write greyscale color table
for (int i = 255; i >= 0; i--) {
writeS = (unsigned short)i;
if (writeS == 10) writeS = 9;
outfile.write ((char*)&writeS,sizeof(char)); // b
outfile.write ((char*)&writeS,sizeof(char)); // g
outfile.write ((char*)&writeS,sizeof(char)); // r
writeS = 0;
outfile.write ((char*)&writeS,sizeof(char)); // 0
}
给出:
...0C 0C 0C 00 0B 0B 0B 00 09 09 09 00 09 09 09 00 08 08 08 00...
...0C 0C 0C 00 0B 0B 0B 00 09 09 09 00 09 09 09 00 08 08 08 00...
符号有什么奇怪的地方吗?我错过了任何明显的错误?有没有人遇到过这样的事情?谢谢!
Is there something weird going on there with notation? Any obvious errors that I have missed? Has anyone ran into something like this before? Thanks!
推荐答案
ASCII 中的数字 10"是换行符,\n
.在 C++ 中,这是换行符.
The "number 10" in ASCII is the line feed character, \n
. In C++, this is the newline character.
您显然已将文件作为文本流打开.由于换行在不同平台上的表示方式不同,文本流执行换行转换:读取时将特定于平台的换行表示转换为 \n
,写入时将\n
字符转换为特定于平台的换行符.
You have apparently opened the file as a text stream. Because newlines are represented differently on different platforms, text streams perform newline translation: when reading they translate the platform-specific newline representation into \n
and when writing they translate the \n
character into the platform-specific newline representation.
在 Windows 上,换行符由 \r\n
表示.当您将 \n
写入文本流时,它会被写入为 \r\n
.
On Windows, line breaks are represented by \r\n
. When you write a \n
to the text stream, it gets written as \r\n
.
要写入原始二进制数据,您需要将流作为二进制流打开.这是通过将 ios_base::binary
标志传递给流的构造函数来完成的.
To write raw binary data, you need to open the stream as a binary stream. This is done by passing the ios_base::binary
flag to the constructor of the stream.
这篇关于C++(Visual Studio),不能写数字'10'要归档,所有其他号码都有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!