c++ - 使用 '?' 打印特殊(扩展)ASCII 字符时在输出中获取 `std::cout`

标签 c++ std

我想在控制台打印扩展的 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::wcout << L"";为了这。如果您的代码在其他行中包含 std::cout,则解决方案如下:

当您不需要打印特殊字符时,使用 _setmode(_fileno(stdout), _O_TEXT); 将 stdout 设置回默认文本样式。

关于c++ - 使用 '?' 打印特殊(扩展)ASCII 字符时在输出中获取 `std::cout`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64098108/

相关文章:

c++ - 使用 myclass::operator<(myclass &other) 对 std::list<myclass*> 进行排序

c++ - 转换 std::vectors 的简单模板函数 - "illegal use of this type as an expression"

c++ - 检查给定索引是否存在于 std::vector 中

c++ - 如何在 C/C++ 中使用 WinHTTP 下载文件?

c++ - 如何实时监控不同的 OpenMP 线程?

C++ 三元组查找容器

c++ - C++在目录中对文件名进行排序

c++ - 具有指向对象指针的 std::vector 的未指定行为

c++ - 在不同 DLL 之间传递的标准对象

c++ - 将 MySQL 日期时间转换为 C++ std::time_t