Wcout function does not print french character

I use wcin to store one character in wchar_t. Then I try to print it by calling wcout and the French character "Γ©": but I do not see it on my console.

My compiler is g ++ 4.5.4 and my OS is Ubuntu 12.10 64 bit.

Here is my attempt (wideChars.cpp):

#include <iostream> int main(){ using namespace std; wchar_t aChar; cout << "Enter your char : "; wcin >> aChar; wcout << L"You entered " << aChar << L" .\n"; return 0; } 

When I understand the program:

 $ ./wideChars Enter your char : Γ© You entered . 

So what happened to this code?

+4
source share
1 answer

First add some error checking. Check what wcin.good() returns after input and what wcout.good() returns after typing "You are logged in"? I suspect one of them will return false .

What are your environment variables LANG and LC_* set to?

Then try to fix this by adding this at the top of your main() : wcin.imbue(std::locale("")); wcout.imbue(std::locale("")); wcin.imbue(std::locale("")); wcout.imbue(std::locale(""));

I don’t have my Ubuntu right now, so I'm flying blind here and mostly guessing.

UPDATE

If the above suggestion does not help, try creating a locale like this and imbue() instead of locale.

 std::locale loc ( std::locale (), new std::codecvt_byname<wchar_t, char, std::mbstate_t>(""))); 

Strike>

UPDATE 2

Here is what works for me. The key should also set the locale C. IMHO, this is an error in the implementation of the GNU C ++ standard library. If I am mistaken, setting std::locale::global(""); must also set the locale of the C library.

 #include <iostream> #include <locale> #include <clocale> #define DUMP(x) do { std::wcerr << #x ": " << x << "\n"; } while (0) int main(){ using namespace std; std::locale loc (""); std::locale::global (loc); DUMP(std::setlocale(LC_ALL, NULL)); DUMP(std::setlocale(LC_ALL, "")); wcin.imbue (loc); DUMP (wcin.good()); wchar_t aChar = 0; wcin >> aChar; DUMP (wcin.good()); DUMP ((int)aChar); wcout << L"You entered " << aChar << L" .\n"; return 0; } 

UPDATE 3

I am confused, now I can not play it again, and setting std::locale::global(loc); seems to be doing the right thing wrt / locale. Besides,

+3
source

All Articles