I think this is very
platform-specific to Windows and probably can't be done with "generic" C++ functions alone.
What you actually can do:
1 2 3 4 5 6 7 8 9 10
|
#include <fcntl.h>
#include <io.h>
#include <iostream>
int main()
{
_setmode(_fileno(stdout), _O_U8TEXT);
CStringW input(L"Mu\x00F1oz!");
std::wcout << input.GetString() << std::endl;
}
| |
Note: We can send a wide-string (UTF-16) to
std::wcout
, but – by default – the C Runtime still is going to translate the given string to the local ANSI codepage
before it is send to the
stdout (terminal), thus messing up any Unicode characters that can't be represented in the local ANSI codepage! We change this behavior with the
_setmode()
function and thus force the
stdout to output UTF-8 to the terminal 😏
The required
SetConsoleOutputCP(CP_UTF8)
appears to be
implicit with this solution.
BUT: Be aware that, once
_setmode()
was used to set the
stdout to UTF-8 mode,
only writing to
std::wcout
works, whereas any attempt to write to
std::cout
will cause an abnormal program termination. I don't think Microsoft has ever fixed this... 🙄
See also:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=msvc-170#remarks
________
Also note that certain "foreign" characters
still won't appear correctly in the terminal, even if all of the above is done correctly, because the "monospace" (typewriter)
font used by the terminal may
not support these characters 🙄🙄🙄