Search code examples
c++filetextbinaryfstream

What Makes a Binary Stream Special?


std::fstream has the option to consider streams as binary, rather than textual. What's the difference?

As far as I know, it all depends on how the file is opened in other programs. If I write A to a binary stream, it'll just get converted to 01000001 (65, A's ASCII code) - the exact same representation. That can be read as the letter "A" in text editors, or the binary sequence 01000001 for other programs.

Am I missing something, or does it not make any difference whether a stream is considered binary or not?


Solution

  • On Linux/Unix/Android there is no difference.

    On a Mac OS/X or later there is no difference, but I think older Macs might change an '\n' to an '\r' on reading, and the reverse on writing (only for a Text stream).

    On Windows, for a text stream some characters get treated specially. A '\n' character is written as "\r\n", and a "\r\n" pair is read as '\n'. An '\0x1A' character is treated as "end of file" and terminates reading.

    I think Symbian PalmOS/WebOS behave the same as Windows.

    A binary stream just writes bytes and won't do any transformation on any platform.