C++ (Visual Studio), Can't write the number '10' to file, all other numbers working?

本小妞迷上赌 提交于 2019-12-23 12:25:16

问题


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
}

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...

isntead of:

...0C 0C 0C 00 0B 0B 0B 00 0A 0A 0A 00 09 09 09 00 08 08 08 00...

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
}

gives:

...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!


回答1:


The "number 10" in ASCII is the line feed character, \n. In C++, this is the newline character.

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.

On Windows, line breaks are represented by \r\n. When you write a \n to the text stream, it gets written as \r\n.

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.




回答2:


Character number 10 is the line feed character, which gets converted to a CRLF combination (13 + 10) if the file is opened in text mode on Windows. Open the file in binary mode.




回答3:


The reason this is happening is that you're probably not opening the file in binary mode. When opening the file in normal (text) mode in Windows, any time you write a newline character (which has numeric value 10), the stream will translate it to \r\n, which accounts for why you're seeing the extra bytes when writing out the number 10.

To fix this, open the file in binary mode:

outfile.open(filename, ios::binary);

Hope this helps!




回答4:


There is a big difference between binary and text, as the folks above have said. 10 will be messed up in the stream and there will be missed/extra bytes.

Open in binary!



来源:https://stackoverflow.com/questions/4972815/c-visual-studio-cant-write-the-number-10-to-file-all-other-numbers-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!