【问题标题】:wcout problems on Mac OSXMac OSX 上的 wcout 问题
【发布时间】:2012-08-26 19:32:57
【问题描述】:

我正在尝试使用 unicode 字符在终端中绘制一些简单的方框图。但是我注意到 wcout 不会为方框图字符输出任何内容,甚至不会输出占位符。所以我决定编写下面的程序并找出支持哪些 unicode 字符,发现 wcout 拒绝输出任何高于 255 的内容。我需要做些什么才能使 wcout 正常工作吗?为什么不能访问任何扩展的 unicode 字符?

#include <wchar.h>
#include <locale>
#include <iostream>

using namespace std;

int main()
{
    for (wchar_t c = 0; c < 0xFFFF; c++)
    {
        cout << "Iteration " << (int)c << endl;
        wcout << c << endl << endl;
    }

    return 0;
}

【问题讨论】:

    标签: macos unicode char


    【解决方案1】:

    我不建议使用wcout,因为它不可移植、效率低下(总是执行转码)并且不支持所有 Unicode(例如代理对)。

    相反,您可以使用the open-source {fmt} library 便携式打印Unicode 文本,包括box drawing characters,例如:

    #include <fmt/core.h>
    
    int main() {
      fmt::print("┌────────────────────┐\n"
                 "│   Hello, world!    │\n"
                 "└────────────────────┘\n");
    }
    

    打印 (https://godbolt.org/z/4EP6Yo):

    ┌────────────────────┐
    │   Hello, world!    │
    └────────────────────┘
    

    免责声明:我是 {fmt} 的作者。

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 2013-09-21
      • 2014-09-14
      相关资源
      最近更新 更多