The difference in the connection methods of the Cyrillic alphabet?

Hello! Taught C++ programming and are largely in the field of IT.

Today I found in console Windows, the echo command which will display on the screen some sort of inscription.
99lWUFp5in8.jpg
I decided to try to invoke this command using C++ and the function system. I note that the always use connection variant of the Cyrillic alphabet using SetConsoleCP(1251); from windows.h, as in the code below.
#include "stdafx.h"
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
 system("echo DATA DELETED");
system("pause");
 return 0;
}

The result of the execution of the program turned out to be gibberish coming out when you disconnected the Cyrillic alphabet.
mjQwKFifRvI.jpg
However, when I decided to use in the code, the setlocale function, the display began to meet my goal.
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
 setlocale(LC_ALL, "Russian");
 system("echo DATA DELETED");
system("pause");
 return 0;
}

8XblUj_u3tc.jpg
Based on the experiments above, the question arises: what is the difference between these two options of connection of the Cyrillic alphabet to the project? If anything, I'm asking not only about the result but about the processes occurring in the system when using these functions.
Thanks in advance for the answer.
March 25th 20 at 13:39
1 answer
March 25th 20 at 13:41
Solution
Based on the experiments above, the question arises: what is the difference between these two options of connection of the Cyrillic alphabet to the project?


Almost everyone. Actually, you have two entities - your program and the terminal. Your program reads data from stdin and writes to stdout/stderr. The terminal also deals with the fact that displays data on the screen. For you it is critical that the program transmit data in the charset in which they can expect to see the terminal.

Accordingly, when you call SetConsoleOutputCP(1251), you tell the terminal "expect data encoded in СР1251". This is bad for several reasons:

  • This is purely window function. It is not portable to other platforms
  • СР1251 is a single - byte encoding, and it is better not to use in the 21st century. Need Unicode.
  • Other programs running in the same terminal (for example echo) don't know what you changed the encoding of the terminal. Because you see garbage.


When you call setlocale , you're telling the program "get encoding settings from the environment and give the text in it." As a result, you adapt to the terminal and use the encoding in which it operates. Most likely it will be Unicode. In this embodiment, all the cons become pros - it's portable, it's Unicode, the rest of the program will work fine.

By the way, call setlocale with a parameter, Russian is not quite true. You need to use an empty string"". Interestingly, I was the second time I see the toaster is "Russian". It seems that somewhere on the Internet is walking the wrong example.

Find more questions by tags C++