首页 > 解决方案 > 得到一个'?在使用 `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!":)

标签: c++std

解决方案


对于在 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 设置回默认文本样式。


推荐阅读