Why does ofstream insert a 0x0D byte before 0x0A?

前端 未结 1 868
深忆病人
深忆病人 2020-12-10 15:46

I\'m outputing an array of unsigned characters in C++ using ofstream fout(\"filename\"); but it produces a spurious character in between. This is the part of th

相关标签:
1条回答
  • 2020-12-10 16:22

    Your stream is opening in text mode, and since 0x0A is the line feed (LF) character, that's being converted by your stream to 0x0D 0x0A, i.e. CR/LF.

    Open your stream in binary mode:

    std::ofstream fout("filename", std::ios_base::out | std::ios_base::binary);
    

    Then line ending conversions should not be performed.

    This is usually considered a good idea anyway, as streams can go bizarre w.r.t. flushing when in text mode.

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