unicode - Writing UTF16 file with std::fstream -
is possible imbue std::fstream std::string containing utf-8 encoding can streamed utf-16 file?
i tried following using utf8-to-utf16 facet, result file still utf-8:
std::fstream utf16_stream("test.txt", std::ios_base::trunc | std::ios_base::out); utf16_stream.imbue(std::locale(std::locale(), new codecvt_utf8_utf16<wchar_t, std::codecvt_mode(std::generate_header | std::little_endian)>); std::string utf8_string = "\x54\\xe2\x83\xac\x73\x74"; utf16_stream << utf8_string;
references codecvt_utf8_utf16 facet seem indicate can used read , write utf-8 files, not utf-16 - correct, , if so, there simple way want do?
file streams (by virtue of requirements of std::basic_filebuf
§22.4.1.4.2[locale.codecvt.virtuals]/3
) not support n:m character encoding conversions case utf8 internal / utf16 external.
you'd have build utf-16 string, e.g. using wstring_convert, reinterpret sequence of bytes, , output using usual (non-converting) std::ofstream
.
or, alternatively, convert utf-8 wide first, , use std::codecvt_utf16 produces utf-16 sequence of bytes, , therefore, can used file streams.
Comments
Post a Comment