0
#include <iostream>                           
#include <clocale>                            
#include <string>                             

int main() {                                  
    std::setlocale(LC_ALL, "en_US.utf8");     
    std::wstring str(L"Τὴ γλῶσσα μοῦ ἔδωσαν");
    std::wcout << str << std::endl;           
    std::wcerr << str << std::endl;           
}         

这不会在终端上产生输出。

我怎样才能让它产生 UTF-8 输出?我认为这应该是 C++ 支持的东西。

我知道我正在使用的utfcpp库,但问题是具体是否有 stdlib 方法可以打印出 UTF8。

4

1 回答 1

0

I got it by following this.

std::setlocale(LC_ALL, ""); is the magic incantation.

This worked on my trivial test program. However my full program still completely fails at printing out wstrings.

That is fine (well, no, it is still somewhat alarming), I will avoid relying on stdlib, and do it myself with the help of utfcpp library, which is excellent. This way I can use cout and cerr for everything and all will be well.

于 2013-07-05T03:54:05.863 回答