c++file-ioencodingutf-8wofstream

Read/write unicode c++


Sadly this is the third time this week I have to post a question.

I have to write text to a file with unicode encoding (or UTF8).
This is what I do:

creating wofstream mystream; and then I put a wstring in it like this mystream << L"hello world";

First question: what kind of encoding the stream uses in my case?

Secondly, I want to load my new file, but how to read the lines? The ifstream's getline is not working because the line ends up ruined obviously.


Solution

  • wchar_t, the type that backs wstream and wstring, is platform dependent: 2 bytes on Windows, 4 bytes on some (all?) Linux. So you will end up writing 'Unicode', but exactly which Unicode is subject to many variables. You may write UTF32/UCS4, you may end up with UTF16/UCS2.

    If you want to write using a specific, well controlled encoding (eg. UTF8, or UCS-2LE vs. UCS-2BE to control endianess) then you need something like iconv. You can also use std::locale to imbue a stream, see https://stackoverflow.com/a/1275260/105929.