Codeblock output Chinese garbled code problem

1. Question

When using codeblock today, I found that the Chinese output from the codeblock terminal was garbled during compilation.
When you see the message "To prevent data loss, this file has been saved as UTF-8" in the lower right corner of the codeblock, garbled characters appear.

2. Analyze the reasons

There is a lot of information on the Internet saying:
The reason for the garbled code is that the decoding methods of the system's local encoding and the codeblocks compiler are different. It is a conflict between the GBK encoding method and the UTF-8 encoding method.
I didn't quite understand it at first, but then I analyzed it carefully.

There are two kinds of gibberish:

  • The file is garbled when opened
  • Terminal output garbled characters

The file is garbled when opened

as follows:

    cout<<"±à¼­Æ÷£ºÉèÖÃΪÁËĬ"<<endl;
    cout<<"±àÒëÆ÷£ºÄ¬ÈÏ"<<endl;
    cout << "Hello world!" << endl;

This is related to the encoding method of the editor.
Settings->Editor->Encoding Settings
Insert image description here
We found one here 打开文件时,使用编码, which is the encoding format we use when saving and opening files:
There are two encoding methods we commonly use: UTF-8 and GBK
WINDOWS-936 represents GBK encoding Format, GBK is the encoding format used by Windows;
UTF-8 is the default encoding format of codeblock, and UTF-8 is the encoding format of Linux.

Digression:
It is precisely because of the conflict between these two formats that mingGW provides a GCC-based program development environment under Windows, which simulates the development environment of GCC under Linux on the Windows platform.

Back to the topic, we still use the WINDOWS-936 encoding format more commonly under the Windows system, but it should be noted that it must be “使用此编码”selected “设为默认的编码方式”. In this case, the encoding format of the files you save and open will be the same, and there will be no garbled characters when opening the file. Of course, it is also possible to choose UTF-8, but the encoding method of subsequent compilation also needs to be adjusted.
Insert image description here

What I chose before was that “作为备用编码”this would simply cause codeblock to detect the encoding format first, and then use the encoding format we defined if it is not detected. It stands to reason that there is no problem, but I don't know why my codeblock should automatically recognize the file as UTF-8 encoding, even if the file is GBK. Of course, if a GBK file is opened using UTF-8 parsing, the above garbled characters will appear.

Note : The encoding format of the file is the same as the format set by the codeblock editor when it is saved, and the encoding format cannot be modified after saving.

Terminal output garbled characters

The terminal output garbled characters are related to the compiler:
Settings->Compiler
Insert image description here
Two commands are needed here:

-finput-charset=charset
-fexec-charset=charset

meaning:

-finput-charset=charset specifies what encoding the compiler uses to interpret the input source file, it requiresThe same encoding format as the source file, compilation is possible only if the formats are the same;
if the formats are different, the following error will be reported:
Insert image description here
it cannot be compiled at all.
-finput-charset=The default value of charset is UTF-8;

-fexec-charset=charset is the encoding format used to output to the terminal during compilation.
-fexec-charset=charset is UTF-8 by default, but Windows cannot recognize it, so it needs to be changed to GBK so that garbled characters will not appear.

3. Summary

To sum up, codeblock has two formats for setting encoding:

(1)GBK-GBK-GBK

Settings->Editor->Use encoding WINDOWS-936

Settings->Compiler->Other compiler settings Enter
the following statement:

-finput-charset=GBK
-fexec-charset=GBK

Insert image description here

(2)UTF-8 - UTF-8 - GBK

Settings->Editor->Use encoding UTF-8
Insert image description here
Settings->Compiler->Other compiler settings Enter
the following statement:

-finput-charset=UTF-8
-fexec-charset=GBK

Insert image description here

4.Attention

It is best to open files in both formats with the corresponding encoding format:
that is, files created by UTF-8 - UTF-8 - GBK should also be opened with UTF-8 - UTF-8 - GBK configuration, and
files created by GBK-GBK-GBK Just use the GBK-GBK-GBK configuration to open it.
Otherwise, problems such as files not being found and codes disappearing may occur.

(I found that my codeblock:
UTF-8 - UTF-8 - GBK configuration opens GBK-GBK-GBK files no problem and can display the code content;
GBK-GBK-GBK configuration opens UTF-8 - UTF-8 - GBK files just fine It won’t show up, it scared me at the time
)

Guess you like

Origin blog.csdn.net/mantou_riji/article/details/123596205#comments_28344064