c++ - 得到一个'?在使用 `std::cout` 打印特殊(扩展)ASCII 字符时在输出中
问题描述
我想在控制台中打印扩展的 ascii 字符,我试过这个:
std::cout << "╔══════════╦════════════════════════════════════════════════════════╗\n";
std::cout << "║ ID:678 ║ Name: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ║ Surname: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ║ Gender: ▓▓▓▓▓▓ ║\n";
std::cout << "║ ║ Age: ▓▓▓ ║\n";
std::cout << "║ ║ Tel: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ║ Adress: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ║ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ID Print ║ Hobbies: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ║ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ░░░░░░ ║ Additional İnformations: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ▒▓▓▓▓▒ ║ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ▓▓▓▓▓▓ ║ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "╚══════════╩════════════════════════════════════════════════════════╝\n";
但输出是这样的:
?????????????????????????????????????????????????????????????????????
? ID:678 ? Name: ???????????????????????????????????????? ?
? ? Surname: ???????????????????????????????????????? ?
? ? Gender: ?????? ?
? ? Age: ??? ?
? ? Tel: ??????????????????????????????? ?
? ? Adress: ????????????????????????????????????????? ?
? ? ????????????????????????????????????????????????? ?
? ID Print ? Hobbies: ???????????????????????????????????????? ?
? ? ????????????????????????????????????????????????? ?
? ?????? ? Additional ¦nformations: ???????????????????????? ?
? ?????? ? ????????????????????????????????????????????????? ?
? ?????? ? ????????????????????????????????????????????????? ?
?????????????????????????????????????????????????????????????????????
但是当我尝试这个时:std::cout << char(200);
它成功打印'╔'字符。所以我确定控制台支持扩展的 ascii 字符。
所以; 我怎样才能正确输出这些cout?
编辑:使用std::wcout << L"";
不起作用。
Edit2:程序只能在 Windows 7 或更高版本中运行。
编辑3:
使用此代码打印卡片,
#include <fcntl.h>
#include <io.h>
_setmode(_fileno(stdout), _O_U16TEXT);
但是,我不能打印任何正常的 cout 字符,它给出了错误。(示例错误字符串std::cout << "Hello!"
:)
解决方案
对于在 windows 上写的人,可以应用这个:source( _setmode Windows source )
需要的库:
#include <fcntl.h>
#include <io.h>
要将 cout 转换为 Unicode:
_setmode(_fileno(stdout), _O_U16TEXT);
*重要提示:在这种风格中,你总是应该使用 std:: w cout << L""; 为了这。如果您的代码在其他行中包含 std::cout,则解决方案如下:
_setmode(_fileno(stdout), _O_TEXT);
当您不需要打印特殊字符时,用于将 stdout 设置回默认文本样式。
推荐阅读
- android - 我正在开发 Cordova/Ionic 应用程序,全屏视频播放已停止在 Chrome 73 中工作
- ajax - 我在 django 中对这个 AJAX 做错了什么?
- angular - rxjs-5-to-6 “找不到任何可能的迁移”
- python - Python 运行所需的最少文件/库是什么?
- android - 当我尝试从服务器获取图像时出现 IndexOutOfBoundsException
- firebase - Firebase 功能触发两次
- javascript - 如何在新状态道具中访问其他状态/属性值
- android - 如何使用 tonyodev.fetch2 显示通知
- category-theory - 将“最佳”产品类型映射到“次优”产品类型的独特态射“m”真的是独一无二的吗?
- python - 如何检查方程中运算符的顺序是否正确?