문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

IFAIR it's done in streambuf implementation, codecvt just deals with the locale representation specifics.

It is performed by std::filebuf, if it was open without the ios::binary flag.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top