【问题标题】:How to output unicode in C++ without _setmode如何在没有 _setmode 的情况下在 C++ 中输出 unicode
【发布时间】:2014-11-10 00:19:27
【问题描述】:

我正在尝试在控制台输出中插入一个 unicode 值,在本例中为 \u250F 或 ┏。我四处寻找,人们推荐了各种各样的东西。在讨论我的尝试之前,我使用的是 Windows 和 Visual Studio 2013。

主要错误

当我尝试多个“修复”时,如果未指定,我总是会收到相同的错误:

Debug Assertion Failed!
Program: ...nts\visual studio 2013\Projects\roguelike\Debug\roguelike.exe
File: f:\dd\vctools\crt\crtw32\stdio\fputc.c
Line: 48

Expression: ((_Stream->_flag & _IOSTRG) || ( fn = _fileno(_Stream), 
((_textmode_safe(fn) ==  _IOINFO_TM_ANSI && !_tm_unicode_safe(fn))))

For more information on how your program can cause an assertion failure, 
see the Visual C++ documentation on asserts

(Press Retry to debug the application)

我的尝试

我已尝试执行以下所有操作来输出它:

std::cout << "\u250F";
std::wcout << "\u250F";
std::cout << L"\u250F";
std::wcout << L"\u250F";
std::cout << "┏";
std::wcout << "┏";
std::cout << L"┏";
std::wcout << L"┏";

我如何尝试告诉控制台如何输出 unicode

_setmode(_fileno(stdout), _O_U16TEXT);

似乎是问题所在,真的。

system("chcp 65001");

不会出错,但不会做任何事情

std::locale::global(std::locale("en_US.utf8"));

它会导致参数“非法”的错误

【问题讨论】:

  • fflush(stdout); _setmode(_fileno(stdout), _O_U16TEXT); std::wcout &lt;&lt; L"\u250f"; 适用于我的 VC++ 10。
  • 也可以直接调用Windows API,例如WriteConsoleW.

标签: c++ unicode visual-studio-2013 cout


【解决方案1】:

以下代码对我有用: 我的 ENV:vs2013,win7_x64,系统区域设置为“英语(美国)”

SetConsoleOutputCP(CP_UTF8);
_setmode(_fileno(stdout), _O_U8TEXT);
wprintf(L"Interface %s\r\n", pNP->pszwName);   //pszwName is a wchar_t*

另一个技巧是你的控制台字体,你的控制台字体是否包含字符'┏'?

google 'cmd font fontlink' 将字体添加到您的 cmd 控制台。

在执行程序之前,在 cmd 控制台中运行 'chcp 65001'。

【讨论】:

  • 同时调用SetConsoleOutputCP_setmode 会适得其反。
【解决方案2】:

有同样的问题。通过切换我的预处理器命令的顺序来解决它。

如果您以这种方式包含 io.h 和 fcntl.h:

#include <fcntl.h>  
#include <io.h>

切换它们,使它们变成这样:

#include <io.h>
#include <fcntl.h>

【讨论】:

  • 尝试反转但仍然得到相同的断言错误
猜你喜欢
  • 2021-04-01
  • 1970-01-01
  • 2010-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多