Originally Posted by
nvoigt
Can anybody post some code? UNICODE is tricky to get right. You need wcout, you most likely need a unicode string (one prefixed with L) your source file needs to be saved as unicode and your codepage in the console needs to support the characters you are trying to print. Elysia already pointed out most of it, but without code it's really hard to guess what little detail might have been overlooked.
Here are the permutations that compiled. (The ones I least expected to work, worked correctly.)
Probably not fault of terminal, as the string can be typed there.
Code:
#include<iostream>
using namespace std;
int main()
{
//When file is saved as utf-8 :
//cout<<"মনসিজ"; //Expected Output (Why ?)
//wcout<<"মনসিজ"; //NO output
//wcout<<u8"মনসিজ"; //NO output
//wcout<<L"মনসিজ"; // Output: "?????"
//wcout<<L"\x0987";//Output : "?" (Trying a single char)
////cout<<L"মনসিজ"; //Output: 0x80486a8
//cout<<u8"This is a Unicode Character: \u0987.";
//(Wikipedia example): Worked (Why ?)
//When file is saved as utf-16 :
///FAILS TO BUILD
//with errors like :
//a.cpp:1:2: warning: null character(s) ignored [enabled by default]
//a.cpp:1:3: error: invalid preprocessing directive #i
}
Strangely, cout seems to work with C++11 unicode ..or plain strings containing unicode chars.
I thought stream support for unicode was postponed in C++11 as it was not ready. (Is this gcc specific ?)
Another thing I tried, which did not work(Probably the reason for it being absent in C++11).
file_1 and file_2 are utf-16 files
Code:
basic_ifstream<char16_t> ifs("file_1");
basic_ofstream<char16_t> ofs("file_2");
basic_string<char16_t> temp;
if(ifs && ofs)
{
//ofs<<ifs.rdbuf();
//Gives a Didn't Work #2 (as below) and the output file remains empty.
//This works with normal streams for copying files.
while(getline(ifs,temp)) //
ofs<<temp;
//terminate called after throwing an instance of 'std::bad_cast'
//what(): std::bad_cast
}
else
cout<<"Didn't work #1"<<endl;
if(!(ifs.good()&&ofs.good()))
cout<<"Didn't work #2"<<endl;