cpp work with utf-8
I’m mainly working with cpp, recently. Sometimes, I have some Chinese characters in the input/output stream, the the encoding issue became annoy.
After some search, I found the way out.
the method to make your utf-8 coded program work in GBK scenario.
add the following line to your code.
#include <clocale> #include<stdio.h> char* dummyGlobalVarAA =std::setlocale(LC_ALL, "en_US.UTF-8"); int main(int argc, char **argv) { printf("abc中文\n"); return 0; }
ref
std::setlocale - cppreference.com
https://en.cppreference.com/w/cpp/locale/setlocale
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合终身会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步