0

I use MinGW64 to compile c++ programs. But since I upgraded to Windows 10, I found my c program output Chinese will be garbled code.

I follow the online method, adding a code in the program header: SetConsoleOutputCP(65001);, then it fixes. but I think it's so troublesome to do this for each c++ program. What should I do?

I think this is my system's problem, the same code in Windows 7 is not a problem, I just want to find a more convenient solution instead of adding the same code to every file

There's the code:

#include <iostream>
#include <windows.h>
using namespace std;
int main() 
{
    SetConsoleOutputCP(65001);
    cout << "文本" ; //Output will be garbled code if there's no line above
    return 0; 
}
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
bakabaka
  • 1
  • 2

1 Answers1

1

The console in most operating systems only expects ASCII character input. In order to show some other char set you have to specify that in your code. The SetConsoleOutputCP command sets the "code page" windows should read from. By the way not all versions of windows have the same code for this command.

Please refer to the documentation found here.

The documentation suggests using EnumSystemCodePages to make sure the code for that language exists on that system.

P.S. Your English is very good :)

EDIT

I tested your code on my computer with Visual Studio 2019 and got the following

Warning C4566 character represented by universal-character-name '\u6587' cannot be represented in the current code page (1255)   

even with the SetConsoleOutputCP command you added. I assume you need to have chines installed for this to work. The problem is that I don't have the relevant code page for the windows console to look in for the char set. see this answer and this answer.

medic17
  • 415
  • 5
  • 16