Print unicode string on console using wcout

Hi,

I am facing issues in char to wchar_t conversion when I use japanese text. Please look at following code and met me know where I am doing mistake.

#include <locale>
#include <iostream>
#include <string>
#include <sstream>
using namespace std ;

int main()
{
locale loc("japanese");
wcout.imbue( loc );
char narrow_phrase[] = "私は開発者です";
wchar_t wide_phrase[sizeof(narrow_phrase)];
use_facet< ctype<wchar_t> >(loc).widen (narrow_phrase, narrow_phrase+sizeof(narrow_phrase), wide_phrase );
wcout << wide_phrase << endl;
return 0;
}

wcout is not printing anything on console. If it is not possible, please provide any alternative for the same.

Thanks
sandy


I have no problem printing unicode text as utf-8 with cout (char version, no wchar_t needed) on Ubuntu:

 
cout << "私は開発者です";


The above works as you'd expect on my machine without any locale junk or anything, as long as the file is saved as utf-8. But again I'm using Ubuntu, I don't know if that works on other unices. And I'm certain it doesn't work on Windows.

If that's no good, I'm afraid I don't know of a better way short of using an external library like NCurses (if that even supports Unicode output -- I've never used it myself). I remember trying to get this to work on Windows -- it was a real chore.
Printing text in UTF-8, as in Disch's example, should work as long as the terminal supports it, it's set to the right output encoding, and has fonts to display the glyphs.

PDcurses, and probably ncurses as well, supports Unicode. To be more accurate, it has a wide character type which can be used to pseudo-overloads of functions.
Topic archived. No new replies allowed.