I want to store a Japanese text in a string and write it to a file. I am totally unfamiliar with encoding and there are a lot of data types like wchar_t and wstring in C++ which appear confusing to me. How can I do this?
I am trying to create a well-formed XML file with some CDATA content being Japanese.
Ignore the complexities and pitfalls of wide strings altogether; and ensure that the data you are dealing with is encoded using UTF-8 instead.
In C++, UTF-8-strings can be handled just like extended ASCII strings; unless you happen to actually manipulating them (chopping them up, counting characters, things like that).
If all you care about is gathering, storing and displaying the strings, it is quite simply laughably trivial.
(Without more information about the environment in which you are working, it’s impossible to tell you exactly how you would go about ensuring UTF-8-ness; but that’s really beyond the scope of this question.)
Edit:
In response to comments regarding what you are planning to do (writing an XML file):
When working with XML in particular; it’s very, very simple:
Never Don’t Use UTF-8!, or “N’DUUH!” for short.
In XML, the ASCII-balance will in practice always be such that UTF-8 is the most space-efficient encoding system.
(To wit, if each Japanese character in the document can be matched by an ASCII character, UTF-8 is exactly as efficient as UTF-16, in terms of space. XML element names are traditionally needlessly verbose, and Japanese sentences are notoriously compact; and when adding in indentation, Japanese text will almost always be matched by ASCII in abundance.)