The problem here is that the term "wide character" is somewhat empty.
The standard library, as has been mentioned, will usually only output based on the current locale settings. At best, this will result in unreliable output if someone is running a locale setting other than what you intended. At worst it means you won't be able to print the characters you want at all.
This problem gets even worse when you realize that many wide output systems (like wcout) are
worthless and simply narrow the characters before outputting them.
Bottom line: don't rely on the standard lib to output anything but basic ASCII.
My favored solution to this problem is to take advantage of Unicode. Unicode allows you to output any and all available characters. However... on Windows it means you can't use the standard library for console I/O. And even getting it to work without the standard lib takes some finagling.
Here's a thread from long ago which covered the topic:
http://www.cplusplus.com/forum/windows/9797/
Long story short, you need to do 2 things:
1) Set the console code page to Unicode (I typically prefer UTF-8)
2) Output Unicode via WriteConsole (if you're outputting UTF-8, you would use WriteConsoleA, not WriteConsoleW as you might expect)
Also note that UTF-8 does not use wide characters, so you can use normal chars. But make sure you notice that non-ascii characters will be multi-byte characters. IIRC most Chinese characters will be 3 or 4 bytes.
3) You'll need to have a font installed that can actually display these characters! Not all fonts have all characters. Incompatible fonts will draw some garbage character if they don't support it.
4) You'll need to make sure your console is using such a font. Since consoles are restricted on the fonts they can use, you'll probably have to get GNU Unifont and set it as your console's display font.
Here's code that
should work in theory. I would test it, but unfortunately I can't get a proper Unicode font working with Windows console:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <Windows.h>
#include <cstring>
#include <iostream>
int main()
{
// Step 1: Set console to accept UTF-8 input/output
SetConsoleCP(65001);
SetConsoleOutputCP(65001);
// Step 2: A UTF-8 formatted string
char example[] = "Some unicode chars: ㅉㄆ伴伈佀傉僔"; // be sure to save the file as UTF-8 encoded (no signature, if possible)
int len = strlen(example);
// Step 3: Output it
WriteConsoleA( GetStdHandle(STD_OUTPUT_HANDLE), example, len, NULL, NULL );
//stall
std::cin.get();
}
|