c++iostreamstreambufcodecvt

streams, stream_bufs, codecvt facets and \n to \r\n translation


What part of the C++ IO streams does the \r to \r\n conversion? Is it the stream_buf itself or is it part of the internal to external encoding conversion by codecvt facet?

UPDATE 1

You all say that it is done in streambuf/filebuf. Ok. But how does this arrangement deal with, e.g., external encodings like UTF-16? Then it seems that the file has to be opened with ios::binary flag which disables the translation.


Solution

  • This conversion is not (usually) performed by stream, streambuf, or facet. It is the responsibility the C library code (e.g. fputc()) that is called by streambuf's overflow() and underflow().

    If you need it for some reason (e.g. when implementing a dos2unix routine), there's a handy example in boost.iostreams.

    EDIT: std::filebuf only supports multibyte encodings for text files, e.g UTF-8 or GB18030 or whatever the locale uses. A UTF-16 file would have to be opened in binary mode, as a plain byte stream (which can be interpreted as UTF-16 with C++11's codecvt facilities), and yes the line endings would not get converted.