c - 如何将 UTF-8 字符串写入 Windows 控制台?
问题描述
在 win32 api 中,我可以通过将标准输出模式设置为 _O_U16TEXT 并使用 wprintf 来显示 unicode。但是如何使用 printf 将 UTF-8 字符串写入控制台?
当我使用 _setmode 将 stdout 设置为 _O_U8TEXT 时,它抛出了一个断言错误:
我还尝试将控制台输出代码页设置为 UTF-8,字符被问号替换:
代码:
#include <stdio.h>
#include <Windows.h>
#include <io.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
//SetConsoleOutputCP(CP_UTF8);
//_setmode(_fileno(stdout), _O_U8TEXT);
printf("你好啊!ABC");
}
解决方案
当我使用 _setmode 将 stdout 设置为 _O_U8TEXT 时,它抛出了一个断言错误
printf
当前不支持输出到UNICODE
流中。改为使用wprintf
。
您可以尝试以下代码,看看是否有帮助:
//Sets the output code page used by the console associated with the calling process.
SetConsoleOutputCP(CP_UTF8); //Unicode (UTF-8)
_setmode(_fileno(stdout), _O_U8TEXT); // _O_U16TEXT / _O_WTEXT
wprintf(L"你好啊!ABC");
推荐阅读
- ios - 通过 VPN 连接调用时,iOS 11.3/11.4 上的 API 请求失败
- clickonce - publish.html 中的 clickonce 卸载版本
- perl - 在 perl 中创建新目录
- asp.net-mvc - 剑道网格两种过滤模式
- python - 熊猫条形图总是创建一个空的子图
- azure-data-lake - 在 Azure Data Lake Store 上获取一些没有任何扩展名的额外文件
- angular5 - 当我刷新页面时 cookie 过期我没有看到任何数据
- java - Maven build/pom.xml 中缺少工件错误
- r - 通过绘制值和颜色创建颜色条
- c# - Web api 中 Token 中的额外声明