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

I have a little weird problem here! I am trying to write a color table for an 8 bit Windows 3.x bitmap file. I just want the file to be in grayscale, so I'm 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 that I get with the hex editor, everything looks fine until I get to number 10, which is written like this:

... 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, what is strange is that he does this only on this one number, but even more strange is that when I change the code to skip 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 something strange happening there with the notation? Any obvious errors that I missed? Has anyone come across anything like this before? Thanks!

+4
source share
4 answers

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

You obviously opened the file as a text stream. Since newlines are represented differently on different platforms, text streams perform newline wrapping: when reading, they translate the newline representation to the platform in \n , and when writing, they translate the \n character to the newline representation for the platform.

On Windows, line breaks are represented \r\n . When you write \n to a text stream, it is 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 ios_base::binary constructor.

+14
source

Character number 10 is a line character that converts to CRLF (13 + 10) if the file opens in text mode in Windows. Open the file in binary mode.

+1
source

The reason this happens is because you probably are not opening the file in binary mode. When you open a file in normal (text) mode on Windows every time you write a newline character (which has a numeric value of 10), the stream will translate it to \ r \ n, which explains why you see additional bytes when writing number 10 .

To fix this, open the file in binary mode:

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

Hope this helps!

+1
source

There is a big difference between binary and text, as people said above. 10 will be mixed up in the stream, and extra bytes will be skipped.

Open in binary format!

0
source

Source: https://habr.com/ru/post/1339398/


All Articles