I\'m trying to create bitmap in C. Firstly what i want to do is copy header and all pixels data from other bitmap file. The problem is with number \'10\', which when i\'m readin
You have to use ios::binary
flag when dealing with non-text files, always:
ofstream myfile;
myfile.open ("example.bin", ios::out | ios::app | ios::binary);
If you're using Windows and not specifying the ios::binary
flag when opening your streams here what happens:
13 10
sequences are converted to 10
when reading: corrupt data in memory. If the data is binary in the first place you'd be unlucky to get those sequences but it can happen
10
is converted to 13 10
(CR+LF) when writing. That is more likely to happen and corrupts your output file.
Note: even if it's corrupt, reading as text and writing back as binary fixes the corruption (or replace 13 10
by 10
)