Windows 8 x64; Visual Studio 2012;
我通过书籍学习C++。在这个论坛上,我发现了许多主题关于如何通过C++读写Unicode字符串。但是这些主题并没有被标记为已解决(???)。这真的是C++中的一个很大的问题吗?我尝试了不同的变体 - 它们对我都不起作用:
#include<iostream>
#include<Windows.h>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main() {
// variant 1:
wcout << L"Hello World!" << endl; // displayed
wcout << L"Привет Мир!" << endl;// not displayed
//**********************************************
// variant 2:
SetConsoleOutputCP(CP_UTF8);
wchar_t s[] = L"Hello World (2)!";
int bufferSize = WideCharToMultiByte(CP_UTF8, 0,
s, -1, NULL, 0, NULL, NULL);
char* m = new char[bufferSize];
WideCharToMultiByte(CP_UTF8, 0, s, -1, m,
bufferSize, NULL, NULL);
wprintf(L"%S", m); // valid output
wcout << endl;
printf("%s", m); // valid output
wcout << endl;
wchar_t s2[] = L"Привет мир (2)!";
int bufferSize2 = WideCharToMultiByte(CP_UTF8, 0,
s2, -1, NULL, 0, NULL, NULL);
char* m2 = new char[bufferSize2];
WideCharToMultiByte(CP_UTF8, 0, s2, -1, m2,
bufferSize2, NULL, NULL);
wprintf(L"%S", m2); // invalid output
wcout << endl;
printf("%s", m2); // invalid output
wcout << endl;
//**********************************************
// variant 3 (not working):
_setmode(_fileno(stdout), _O_U16TEXT);
wcout << L"Testing unicode -- English -- Ελληνικά"
<< "-- Español." << endl;
return 0;
}
但它仅适用于英文字符... 屏幕: