I’ve been struggling through unicode support in C++, and am getting strange behaviour. If I load a line of unicode text from file, I can store it inside a regular string and output it to stdout without problems. But if I use a unicode string literal with the exact same characters, I have to store it in a wstring and it fails to output properly. Why?
Why is it possible to get unicode in a string instead of needing a wstring? Why does it cout correctly and wstring fails to wcout?
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(int argc,
char* argv[])
{
ifstream infile("unicode.txt");
string strFromFile;
getline(infile, strFromFile);
infile.close();
cout << "From file: " << strFromFile << endl;
wstring strLiteral = L"ĀƁĊĐĒƑĢĦĪĴĶŁΜŊŌƤǬŖŞŦŪƲŴΧɎƵāƂċđēƒġħıĵķłɱŋōƥǭŗşŧūνŵχɏƶ";
wcout << "From literal: " << strLiteral << endl;
return 0;
}
And the output:
From file: ĀƁĊĐĒƑĢĦĪĴĶŁΜŊŌƤǬŖŞŦŪƲŴΧɎƵāƂċđēƒġħıĵķłɱŋōƥǭŗşŧūνŵχɏƶ
From literal: �
�"&*46A�JL��V^fj�t�N��
�!'157BqKM��W_gk�u�O�
I compiled this example with:
g++ main.cpp -Wall
And my compiler is:
g++ (Ubuntu 13.2.0-4ubuntu3) 13.2.0