c++ 中文乱码问题

 

参考文章:https://www.cnblogs.com/CodeWorkerLiMing/p/12503166.html

问题分析:Windows(中文)默认的字符集是Windows-936(GBK),而GCC编译器默认编译的时候是按照UTF-8解析的,造成乱码

chcp 查看window默认编码 936 -> GBK ;; system("chcp 65001"); #UTF-8

 

问题描述

1 win中文无法显示问题

测试代码

#include<iostream>
using namespace std;
int main(){
    cout<<"中文无法显示"<<endl;
    return 0;
}

解决

g++ -fexec-charset=GBK test1.cpp

 

2 fstream读取test.txt文件显示乱码

test.txt

1 王华 1
22

main.cpp

复制代码
#include<iostream>
#include<string>
using namespace std;
#include <fstream>


void file_r()
{
    ifstream ifs;
    ifs.open("test.txt", ios::in);
    int id;
    string name;
    string dId;
    while (ifs >> id && ifs >> name && ifs >> dId)
    {
        cout<<id<<" "<<name<<" "<<dId<<endl;
    }
}
int main(){
    cout<<"读取的文件显示乱码"<<endl;
    string a;
    file_r();//读文件
    return 0;
}
复制代码

 

解决:记事本打开test.txt另存为,修改编码ANSI

 

 

编译执行,正常

 

 

 

over!!

 

posted @   风hua  阅读(1631)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示