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.
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);
}
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.
std::wcoutand 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