How to write Cyrillic text in C++ console

Viewed 48170

For example, if I write:

cout << "Привет!" << endl; //it's hello in Russian

In the console it would be something like ╧ЁштхЄ!.

OK, I know that we can use:

setlocale(LC_ALL, "Russian");

But after that, command line arguments in Russian do not work (if I start my program through a BAT file):

StartProgram.bat

chcp 1251
MyProgram.exe -user=Олег -password=Пароль

So, after setlocale the program can't read Russian arguments properly.

This happens because the BAT file in CP1251, but the console is in CP866.

So, there is a question:

How can I write Russian text in the C++ console and at the same time have Russian command line arguments read properly.

8 Answers

Have you tried using wcout? It is similar to cout, but it accepts "wide" characters, which should permit the proper unicode encodings.

This article about localization, and another, both from MSDN may be of use.

Have you set the language for non-unicode programs to be Russian, in the Regional and Language Options section of the Control Panel?

(I have no idea what the usual setup for Russian-speaking programmers might be; I just wonder whether it is common to set this to some kind of English to avoid confusing overly-parochial tools.)

Unless my memory is playing tricks, when I was working with some code from Japanese developers it was this step that got the console displaying non-Unicode Japanese text (Shift-JIS encoding) properly.

Related