代码先锋网 代码片段及技术文章聚合

visual studio 2019中文乱码

技术标签: c++

1 编码

(1) code编码

vs 中代码文件编码形式 默认 改成utf-8 使用插件 Force UTF-8 (No BOM)

  • 选择扩展-管理扩展后下载对应插件(在搜索框输入UTF-8),将VS的输出编码更改为UTF-8

(2) 控制台编码

(3) txt文件编码

(4) 控制台编码

  1. WIN+R打开,输入regedit
  2. 计算机\HKEY_CURRENT_USER\Console\下的与Visual Studio相关的文件夹,把 Codepage 项的值 65001 改成 936 就可以了。

2 中文输出

两种方式 locale _setmode

(1) 更改 locale 显示中文

locale 详细解释

在main函数中,首先执行了这两行代码对地区环境进行了初始化——

	#include <locale>
    locale::global(locale(""));
    wcout.imbue(locale(""));
  1. locale(“”):调用构造函数创建一个local,其中的空字符串具有特殊含义:使用客户环境中缺省的locale(《C++标准程序库—自修教程与参考手册》P697)。例如在简体中文系统上,会返回简体中文的locale。
  2. locale::global(locale(“”)):将“C++标准IO库的全局locale”设为“客户环境中缺省的locale”。注意它还会设置C标准库的locale环境,造成与“setlocale(LC_ALL, “”)”类似的效果(《C++标准程序库—自修教程与参考手册》P698)。
  3. wcout.imbue(locale(“”)):使wcout使用“客户环境中缺省的locale”。

就这样,使C标准库、C++标准IO库(尤其是wcout)均正确的设置了地区环境,与客户环境中缺省环境完全匹配。

1).coutwcout

csdn:C++_wcout / C_wprintf (wcout输出中文)
csdn:wcout

  • cout是用来输出char的,如果用cout输出wchar,只会输出地址,要使用wcout输出wchar

  • wcout默认是不输出中文的,要想输出中文,要先加入setlocale( LC_ALL, "chs" );这行代码。

在缺省的C locale下,cout可以直接输出中文,但对于wcout却不行。对于wcout,需要将其locale设为本地语言才能输出中文:

	std::wcout.imbue(std::locale(std::locale(),"",LC_CTYPE)); // ①

也有人用如下语句的,但这会改变wcout的所有locale设置,比如数字“1234”会输出为“1,234”

	std::wcout.imbue(locale(""));

2). ofstream wofstream

在缺省的C locale下,ofstream能正确输出中文到文件中,但不支持中文文件名;wofstream支持中文文件名,但不能向文件中输出中文。要解决这个 问题,需要在打开文件之前将全局locale设为本地语言。将全局locale设为本地语言后,ofstreamwofstream的问题都解决了,但 coutwcout却不能输出中文了。要让cout和wcout输出中文,需要将全局locale恢复原来的设置,如下所示:

  locale &loc=locale::global(locale(locale(),"",LC_CTYPE)); // ②
  ofstream ofs("ofs测试.txt");
  wofstream wofs(L"wofs测试.txt");
  locale::global(loc); // ③
  ofs<<"test测试"<<1234<<endl;
  wofs<<L"Another test还是测试"<<1234<<endl;

3). printfwprintf

加上这两位C语言中的老兄,问题更加复杂。考虑如下语句(注意s的大小写):

   printf("%s", "multibyte中文/n");  // ④
   printf("%S", L"unicode中文/n");  // ⑤
   wprintf(L"%S", "multibyte中文/n");  // ⑥
   wprintf(L"%s", L"unicode中文/n");  // ⑦

缺省情况下,⑤、⑦两条语句不能输出中文,这两条语句中字符串的形式是unicode形式的。如果在所有输出语句之前加上如下语句将C语言的全局locale设置为本地语言(C语言中只有全局locale)就可以正常输出了:

  setlocale(LC_CTYPE, "");  // ⑧

但这会导致cout和wcout不能输出中文(汗,的确麻烦),将C语言的全局locale恢复后cout和wcout就正常了,如下所示:

  setlocale(LC_CTYPE, "C");  // ⑨

(2) 通过 _setmode()输出中文

c++中用wcout输出带有中文的wstring为什么会中文乱码?

# include<io.h>
# include<fcntl.h>
_setmode(_fileno(stdout), _O_WTEXT);
std::wcout << L"测试";
wprintf(L"测试");
  1. _O_WTEXT = 0x10000 <fcntl.h>里定义,如果不想加载<fcntl.h>直接用0x10000代替也可以
  2. 注意:如果后面要使用std::coutprint()的话需要在之前运行一次

设置 _setmode后 用std::cout 输出Eigen报错, 使用第一种方法后好了 ,参考文献给了另一种解决办法,并未尝试
stackflow:How to print UTF-8 strings to std::cout on Windows?

Microsoft Visual C++ Runtime Library
Debug Assertion Failed!
Program: d:\visual studio 2015\Projects\utf8test\Debug\utf8test.exe File: minkernel\crts\ucrt\src\appcrt\stdio\fputc.cpp Line: 47
Expression: ( (_Stream.is_string_backed()) || (fn = _fileno(_Stream.public_stream()), 
((_textmode_safe(fn) == __crt_lowio_text_mode::ansi) && !_tm_unicode_safe(fn))))

3 中文乱码问题

中文输出乱码通常是由于编码不符合产生的。c++文件编码于控制台不相符
使用wcout输出中文时

  • 不设置 显示乱码
  • locale 遇到编码不符合的情况会检查到错误,并不输出
  • _setmode 不会检查编码错误,直接输出乱码

(1) 代码与编程环境编码

/utf-8 (Set source and execution character sets to UTF-8)
You can use the /utf-8 option to specify both the source and execution character sets as encoded by using UTF-8. It’s equivalent to specifying /source-charset:utf-8 /execution-charset:utf-8 on the command line. Any of these options also enables the /validate-charset option by default. For a list of supported code page identifiers and character set names, see Code Page Identifiers.

By default, Visual Studio detects a byte-order mark to determine if the source file is in an encoded Unicode format, for example, UTF-16 or UTF-8. If no byte-order mark is found, it assumes that the source file is encoded in the current user code page, unless you’ve specified a code page by using /utf-8 or the /source-charset option. Visual Studio allows you to save your C++ source code in any of several character encodings. For information about source and execution character sets, see Character sets in the language documentation.

To set this compiler option in the Visual Studio development environment

  1. Open the project Property Pages dialog box.
  2. Select the Configuration Properties > C/C++ > Command Line property page.
  3. In Additional Options, add the /utf-8 option to specify your preferred encoding.
  4. Choose OK to save your changes.

(2) 文件编码

Visual C++文本文件读写编码问题

在C++程序中读写文件时,有两个因素涉及到文本编码。一是文本内容的编码,二是文件路径的编码。这两个问题如果不处理好,就可能会出现乱码或者找不到文件的问题。

#include <ifstream>
#include <locale>
	std::ifstream f;
	f.open("UTF-8.txt");
	f.imbue(std::locale(".65001")); 

    f.open("GBK.txt");
    f.imbue(std::locale(".936"));

设置UTF-8编码文件GBK编码文件
fstream(默认)原始数据原始数据
fstream(设置了locale)原始数据原始数据
wfstream(默认)原始数据原始数据
wfstream(设置了locale)转换为UTF-16(程序中的宽字符类型编码)转换为UTF-16 (程序中的宽字符编码)

(3) 文件路径编码

版权声明:本文为guchaono1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/guchaono1/article/details/126480605

智能推荐

Visual Studio 2019许可证授权

Visual Studio 2019许可证如下,亲测有效: Visual Studio 2019 Enterprise(企业版) Visual Studio 2019 Professional(专业版)...

visual studio 2019 禁用 上传数据

需要注意 Change this path if you are using Community or Professional editions 这是一个批处理文件 并非本人原创  出处见...

wordcloud库安装 visual studio 2019安装

昨天pre用到了词云,但比较紧急只好用在线网站,属实吃藕,于是决定自己写个python程序,一顿操作pip 结果发现……对!它又报错了!!!我真服了 干啥啥不行🙄 报错的原因看了一下大概是没有cpp编译的意思吧~ 这里有两种方法: 选择https://www.lfd.uci.edu/~gohlke/pythonlibs/,这个是大神已经编译完的python第三方库,相...

visual studio 2019编辑结构体

visual studio 2019编辑结构体 利用visual studio 2019编辑结构体有警告? 代码示意如下:...

Visual Studio 2019 创建CMake Linux项目

之前我有一篇文章讲的是使用Visual Studio 2017 创建Linux C++ 项目(使用VS2017调试Linux C++代码),使用IDE的好处就是方便编码和调试。工作中也一直以这种方式开发Linux平台的项目,使用了大概两年多,总体上可以满足开发需求,但是还存在一些bug,或者说令人不太满意的地方。 问题1:本地文件和远程同步问题 项目中如果有脚本或者配置等文本文件,由于这些文件不参...

猜你喜欢

Visual Studio 2019 离线安装教程

Visual Studio 2019 离线安装教程 Visual Studio 2019 经过精心设计,可在各种网络和计算机配置中良好运行。 虽然我们建议你试用 Visual Studio Web 安装程序—这是一个小巧文件,可及时提供最新修补程序和功能—但我们知道对你而言这也许并不可行。 例如,你的 Internet 连接不可靠或带宽较低。 ...

libtiff配置心得 & Visual studio 2019

1.编译 在Visual Studio 2019命令行里面输入,: 1.【到E:\Open_source\tiff-4.1.0\libtiff】:E:  回车, cd E:\Open_source\tiff-4.1.0\libtiff 3.【编译】:nmake /f makefile.vc   编译后会得到两种库:libtiff.lib 和 libtiff_i.lib...

Visual Studio2019配置LibTorch

1、下载对应版本LibTorch,只用作预测下载CPU版本即可:官方网站 2、Visual Studio中,配置管理器,将配置改成所有配置,平台改为所有平台。 3、Visual Studio中,项目>属性>C/C++>常规>附加包含目录,添加: 4、Visual Studio中,项目>属性>链接器>常规>链接库依赖目录项,添加: 5、Visual S...