This question has already been answered:

I recently started learning C ++, I have a Dev C ++ compiler. When i do

std::cout<<"Привет!"; 

it turns out abracadabra! And if I loop through the characters, the Russian letters are displayed correctly. I think the problem is in the difference between the dos and windows character sets. Who knows some Dev C ++ gadgets (like keycyr for pascal) or C ++ operators / functions / procedures that allow you to change the encoding of std :: cout.

Reported as a duplicate by Abyx , aleksandr barakin , user194374, Nick Volynkin 4 Feb '16 at 6:01 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • You can usually use std::wcout and the rest wide-types / methods. However, as far as I remember, in Dev-C ++ the MinGW is used by default for compiling code, they are simply not there . - AlexeyM
  • one
    Why are you crushing this crutch everywhere? :-( - karmadro4
  • Because it is THE BEST and not “everywhere” but in two interrelated issues because I want to help the same people as I have Hashcode if no one could help me! - Rules
  • 3
    @Rules, most already have a working way to deal with this problem without using a modified compiler. If you could not take advantage of their experience, it does not mean that they could not help you. - insolor
  • @Rules, in UPDATE, led to its old answer a new input-output method with the console, based on the @insolor response. It comes down to calling one function at the very beginning of main (). Then you just work with printf () / fgets () / ... - Look, maybe you will like it. - avp

5 answers 5

With setlocale() on science correctly, but if the localization is crooked (as I have on this machine), it does not work.

On Windows, there are 2 functions, CharToOem(char, char) and OemToChar(char, char) for translating cp-1251 to cp-866 (console) and back.

Example:

 #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> main () { char str[1000], buf[1000]; int itty = isatty(fileno(stdin)), otty = isatty(fileno(stdout)); strcpy(str, "привет"); if(otty) CharToOem(str, str); puts(str); fgets(str, 1000, stdin); if (itty) OemToChar(str, str); str[strlen(str) - 1] = `\0`; if (strstr("привет", str)) strcpy(buf, "да, часть от 'привет'"); else strcpy(buf, "такого нет в слове 'привет'"); if(otty) CharToOem(buf, buf); puts(buf); } 

The program is typed in cp-1251 (also known as ANSI ).

When typing from the console, regardless of setlocale() will have to translate the encoding. Also note that the command line parameters typed in the console enter the program in cp-1251 encoding !!!

Encoding in files is usually implied in cp-1251 , this should be taken into account when redirecting ( > ) output ( stdout , stderr ). I demonstrated in the example how this can be done.

UPDATE

@insolor gave a wonderful answer on how to properly program work with Russian letters in the Windows console, using standard functions
SetConsoleCP(1251) and SetConsoleOutputCP(1251) .

In the discussion (comments) to his answer, you can find various details, in particular the installation of the Russian font.

It looks like his answer went unnoticed for some forum participants. Therefore, I decided to give a function, the call of which at the very beginning simplifies the programming of the I / O of Russian letters in Windows and an example of its use.

  1. cons1251.c

     #include <windows.h> static int oldin = 0, oldout = 0; static void exitfunc() { SetConsoleCP(oldin); SetConsoleOutputCP(oldout); } void cons1251 () { if (oldin) return; atexit(exitfunc); oldin = GetConsoleCP(); oldout = GetConsoleOutputCP(); SetConsoleCP(1251); SetConsoleOutputCP(1251); 

    }

  2. rustest.c

     #include <stdio.h> // русский текст в кодировке cp1251 (она же ANSI) // обратите внимание, <windows.h> не нужен main() { printf ("This is russian [аБвГдЕ] before cons1251()\n"); cons1251(); printf ("This is russian [аБвГдЕ] after cons1251()\n"); char str[1000], buf[1000]; printf ("Привет - введи часть от \"привет\"\n"); fflush(stdout); fgets(str,1000,stdin); str[strlen(str)-1] = 0; printf("Вы ввели: '%s'\n",str); if(strstr("привет",str)) printf("да, часть от 'привет'\n"); else printf("такого нет в слове 'привет'\n"); fflush(stdout); puts("ждем ввода to exit..."); fflush(stdout); fgets(str,1000,stdin); } 

Calling fflush(stdout) not necessary when working with the console (tty), but is required for output in some terminal emulators (for example in Emacs eshell).

This is an example of compilation (Windows 7, Emacs eshell) window in cp1251 therefore the output of the Russian text both before and after cons1251() same (and correct).

 c:/Users/avp/src/cc/hashcode $ gcc -c cons1251.cc:/Users/avp/src/cc/hashcode $ gcc rustest.c cons1251.o -o rustest c:/Users/avp/src/cc/hashcode $ ./rustest This is russian [аБвГдЕ] before cons1251() This is russian [аБвГдЕ] after cons1251() Привет - введи часть от "привет" иве Вы ввели: 'иве' да, часть от 'привет' ждем ввода to exit... c:/Users/avp/src/cc/hashcode $ c:/Users/avp/src/cc/hashcode $ 

Unfortunately for the standard cmd-window Copy / Paste does not work, so try it yourself.

For the window with PowerShell in Win 7, this method unfortunately does not work (unlike the “hemorrhoid” CharToOem / OemToChar ).

I hope this text will be useful to someone.

UPDATE 2 (for @Rules)

Source file tt.cpp

 #include <iostream> extern "C" void cons1251(void); main() { std::cout << "Привет, введите слово\n"; cons1251(); std::cout << "Привет, введите слово\n"; char str[100]; std::cin >> str; std::cout << "Вы ввели: " << str << std::endl; } 
 c:/Documents and Settings/avp/src/hashcode $ g++ tt.cpp cons1251.o -o tt c:/Documents and Settings/avp/src/hashcode $ ./tt Привет, введите слово Привет, введите слово йцукен Вы ввели: йцукен c:/Documents and Settings/avp/src/hashcode $ 

Example output from the emulator terminal Emacs eshell. Therefore, the first "Hello ..." also consists of normal letters. In an ordinary console, there are “cracks”, and then normal Cyrillic.

Pay special attention . Before launching, you need to change the font in the console on the Lucida Console . Otherwise there will be krakozyabry (but others). To replace the font, right-click on the inscription "Command Line" console window. In the menu that opens, select "Properties" . In the window that appears, select the “Font” tab and select “Lucida Console” there . Then you confirm that he will.

If during confirmation you select the item “Change shortcut to launch this window” , then the new command prompt windows will already be with the required font.

Look like that's it.

    1. You can immediately edit the source code in cp866 encoding, for this you need any editor that supports this encoding. For example, you can edit in the far manager's built-in editor.
    2. In the Maine, we insert the calls of two functions: SetConsoleCP (1251); SetConsoleOutputCP (1251); and when you first start install the console font on the Lucida Console or Consolas.
    • How to install the font? Right-click in the upper frame -> Properties -> Font ?? And if the program was called up by double clicking on the icon (along with the console), but should it issue a greeting ??? - Typing text in cp-866 will not save from recoding (files, arguments, environment variables). - avp
    • Yes, the font is set as follows. And for the program a shortcut is made with the desired console font. PS At least it saves from the need for constant transcoding when outputting to the console. - insolor
    • Do you still convert the input from the console with your hands, or does SetConsoleCP (1251) enter cp-1251? - I tried, switched the font, called chcp 1251, in Russian (in the sense of cp-1251) and enters and outputs. The only question is what to do with the programs already written for the console in cp-866? More confusion will be. - @insolor, and you don’t know how you can determine from the program in which encoding the console is located? It would be better and input and output. - avp
    • one
      > Do you still convert the input from the console with your hands, or does SetConsoleCP (1251) enter cp-1251? SetConsoleCP (1251) enters cp1251, and SetConsoleOutputCP (1251); displays. With already written programs in the presence of the source, you can do something (if you really need). > How can you determine from the program in what encoding is the console? It would be better and input and output. Obviously, the functions GetConsoleCP () and GetConsoleOutputCP () (input and output, respectively) :) - insolor
    • one
      I tried again. It works well. Put the font Lucida Console and about a miracle : the text in cp-866 is normal, then the program calls SetConsoleCP (1251) and SetConsoleOutputCP (1251) and normally writes and reads in cp-1251 (I do not check anything !!!, the redirected stdout is also OK) . Restore (SetConsole ...), memorized earlier (GetConsole ...) installation and again EVERYTHING works as before in cp-866 . Only the font itself is ugly. - avp

    You need to change the locale encoding through the setLocale(): setlocale(LC_ALL,"Russian") function setLocale(): setlocale(LC_ALL,"Russian") . More detailed description: setlocale .

    • It is also very desirable to restore the original locale after the output of the Russian text so that there are no problems with the encoding in other parts of the program. - vladimir_ki
    • one
      > It is also very desirable to restore the original locale after the output of the Russian text, so that there are no problems with the encoding in other parts of the program. Console programs are usually small, so there should be no problems. And if large, the output is usually always in one place. No one will make a conclusion in different languages! Russian and English should be enough. - gammaker
    • In this case, you are right, @GLmonster, and also think that for @Rules, if he has just started learning C ++, it will be useful to learn how to deal with the locale in a more general case, since after writing a console utility, he may suddenly move on to something bigger. - vladimir_ki

    Interestingly, why not just use the Russian locale (?):

     ... #include<clocale> ... int main() { setlocale(LC_ALL,"Russian"); // русская локаль } 

      Russian language in source code in Dev C ++

      It has nothing to do with the development environment and the compiler.

      This question is quite common. Why ask him once again if he is easily googled? Here are some links: