visual studio 2019中文乱码

文章目录

  • 1 编码
    • (1) code编码
    • (2) 控制台编码
    • (3) txt文件编码
    • (4) 控制台编码
  • 2 中文输出
    • (1) 更改 `locale` 显示中文
      • 1).`cout` 与 `wcout`
      • 2). `ofstream ` 与 `wofstream`
      • 3). `printf` 和 `wprintf`
    • (2) 通过 `_setmode()`输出中文
  • 3 中文乱码问题
    • (1) 代码与编程环境编码
    • (2) 文件编码
    • (3) 文件路径编码

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::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
# include
_setmode(_fileno(stdout), _O_WTEXT);
std::wcout << L"测试";
wprintf(L"测试");
  1. _O_WTEXT = 0x10000 里定义,如果不想加载直接用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 
#include 
	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) 文件路径编码

你可能感兴趣的:(c++)