为什么我的C 程序输出乱码代码

Why does my c++ program output garbled code

本文关键字:代码 输出 程序 我的 为什么      更新时间:2023-10-16

i使用 mingw64 来编译C 程序。但是,由于我升级到Windows 10,因此我发现我的C程序输出中文将是乱码的代码。

我遵循在线方法,在程序标题中添加代码: setConsoleoutputcp(65001(; ,然后修复。但是我认为每个C 程序都非常麻烦。我该怎么办?

我认为这是我的系统问题,Windows 7中的代码不是问题,我只想找到一个更方便的解决方案,而不是在每个文件中添加相同的代码

有代码:

#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; 
}

大多数操作系统中的控制台仅期望ASCII字符输入。为了显示其他一些字符集,您必须在代码中指定这一点。setConsoleoutputcp命令设置"代码页" Windows应该从中读取。顺便说一下,并非所有版本的Windows都具有相同的代码。

请参阅此处找到的文档。

该文档建议使用EnumsystemCodepages确保该语言的代码在该系统上存在。

P.S。你的英语很好:)

编辑

我使用Visual Studio 2019在计算机上测试了您的代码,并获得了以下内容

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

即使使用setConsoleoutputcp命令,您也添加了。我认为您需要安装Chines才能正常工作。问题在于,我没有Windows控制台的相关代码页面来寻找Char集。请参阅此答案和此答案。