gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:28:58 26 4
gpt4 key购买 nike

无法让“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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com