I’m trying to write a wstring to file with ofstream in binary mode, but I think I’m doing something wrong. This is what I’ve tried:
ofstream outFile('test.txt', std::ios::out | std::ios::binary); wstring hello = L'hello'; outFile.write((char *) hello.c_str(), hello.length() * sizeof(wchar_t)); outFile.close();
Opening test.txt in for example Firefox with encoding set to UTF16 it will show as:
h�e�l�l�o�
Could anyone tell me why this happens?
EDIT:
Opening the file in a hex editor I get:
FF FE 68 00 00 00 65 00 00 00 6C 00 00 00 6C 00 00 00 6F 00 00 00
Looks like I get two extra bytes in between every character for some reason?
I suspect that sizeof(wchar_t) is 4 in your environment – i.e. it’s writing out UTF-32/UCS-4 instead of UTF-16. That’s certainly what the hex dump looks like.
That’s easy enough to test (just print out sizeof(wchar_t)) but I’m pretty sure it’s what’s going on.
To go from a UTF-32 wstring to UTF-16 you’ll need to apply a proper encoding, as surrogate pairs come into play.