Creating bitmap in C - How to write /r /n as 1 byte?

前端 未结 1 1109
迷失自我
迷失自我 2021-01-29 06:58

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

相关标签:
1条回答
  • 2021-01-29 07:45

    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)

    0 讨论(0)
提交回复
热议问题