c++ 无法获取 "wcout"来打印 unicode,并让 "cout"继续工作

标签 c++ windows unicode utf-8 cout

无法让“wcout”在多个代码页中打印 unicode 字符串,同时让“cout”继续工作

请帮我让这 3 行一起工作。

std::wcout<<"abc "<<L'\u240d'<<" defg "<<L'א'<<" hijk"<<std::endl;
std::cout<<"hello world from cout! \n";
std::wcout<<"hello world from wcout! \n";

输出:

abc hello world from cout!

我试过:

#include <io.h> 
#include <fcntl.h>
_setmode(_fileno(stdout), _O_U8TEXT);

问题: “wcout”失败

尝试过:

std::locale mylocale("");
std::wcout.imbue(mylocale);

和:

SetConsoleOutputCP(1251);

setlocale(LC_ALL, "");

SetConsoleCP(CP_UTF8)

没有效果

最佳答案

C++ 说:

[C++11: 27.4.1/3]: Mixing operations on corresponding wide- and narrow-character streams follows the same semantics as mixing such operations on FILEs, as specified in Amendment 1 of the ISO C standard.

the referenced document说:

The definition of a stream was changed to include the concept of an orientation for both text and binary streams. After a stream is associated with a file, but before any operations are performed on the stream, the stream is without orientation. If a wide-character input or output function is applied to a stream without orientation, the stream becomes wide-oriented. Likewise, if a byte input or output operation is applied to a stream with orientation, the stream becomes byte-oriented. Thereafter, only the fwide() or freopen() functions can alter the orientation of a stream.

Byte input/output functions shall not be applied to a wide-oriented stream and wide-character input/output functions shall not be applied to a byte-oriented stream.

根据我的解释,简而言之,不要混合使用 std::coutstd::wcout

关于c++ 无法获取 "wcout"来打印 unicode,并让 "cout"继续工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22950412/

相关文章:

python - 使用 pygame 显示 unicode 符号

c++ - 当我们有 new/delete 时,为什么要使用 malloc/free?

c++ - 为什么标准不允许在模板参数列表中初始化常量依赖类型?

c++ - 为什么在类对象数组上调用 delete 而不是 delete[] 会导致堆损坏?

c# - 写入未插入的 USB 设备

c# - 如何确定CSV文件是否为unicode

c++ - 在引用 C++ 中存储函数的返回值

c++ - COBOL 到 C++ 数据转换

windows - xperf 调用堆栈跟踪,特定于 dll

java - 使用正则表达式搜索 unicode 文本