C++ File I/O through File Streams
Binary and Text Modes
A text stream is an ordered sequence of characters that can be composed into lines; a line can be decomposed into zero or more characters plus a terminating '\n' (newline
) character. Whether the last line requires a terminating '\n' is implementation-defined. Furthermore, characters may have to be added, altered, or deleted on input and output to conform to the conventions for representing text in the OS (in particular, C streams on Windows OS convert '\n' to '\r\n' on output, and convert '\r\n' to '\n' on input).
Data read in from a text stream is guaranteed to compare equal to the data that were earlier written out to that stream only if...
Data read in from a text stream is guaranteed to compare equal to the data that were earlier written out to that stream only if each of the following is true:
- The data consist of only printing characters and/or the control characters '\t' and '\n' (in particular, on Windows OS, the character '\0x1A' terminates input).
- No '\n' character is immediately preceded by space characters (such space characters may disappear when such output is later read as input).
- The last character is '\n'.
A binary stream is an ordered sequence of characters that can transparently record internal data. Data read in from a binary stream always equal the data that were earlier written out to that stream, except that an implementation is allowed to append an indeterminate number of null characters to the end of the stream.
A wide binary stream doesn't need to end in the initial shift state.
basic_streambuf
is a class template that abstracts a raw device, whereas basic_filebuf
is a class template that implements raw file device.