Advertisement

Unicode problem

Started by February 11, 2023 10:03 AM
1 comment, last by Alberth 1 year, 10 months ago

I'm trying to learn Unicode. I wrote a simple test program:

#include <iostream>  
#include <string>  

int main()
{
std::wstring greekWord = L"Ελληνικά";
std::wcout << greekWord << std::endl;
return 0;
}

However, when I run it, I don't see anything printed out. Anyone know what is going wrong?

Thank you.

Does plain ASCII output work?

“not getting output” is suspicious when printing text. Falling back to the simpler case may show whether there is a more basic problem somewhere.

By compiling and running the program I get an ASCII equivalent as output:

%  g++ -Wall x.cpp
%  ./a.out
Ellinika

which is quite interesting that it can even do that :D

Assuming my locale settings are non-optimal, I borrowed some locale settings from an example in cppreference https://en.cppreference.com/w/cpp/locale/setlocale​ and putting your code inside, I get

%  g++ -Wall x.cpp
%  ./a.out        
New LC_ALL locale: en_US.UTF-8
Ελληνικά
Restorred LC_ALL locale: C

which is likely what you'd expect.

Hard-coding a locale works, but is likely a bad idea. So rather than forcing the program with a hard-coded locale, you'll want to fix this by configuring your environment properly. In that way, if a different person uses the program it will adapt itself to the locale of the different system. In particular it will not push text that the environment does not know how to handle.

In my case, the “C” locale that I have says it understands ASCII only, so no wonder I didn't get non-ASCII output.

This topic is closed to new replies.

Advertisement