无法让“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 onFILE
s, as specified in Amendment 1 of the ISO C standard.
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()
orfreopen()
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::cout
和 std::wcout
。
关于c++ 无法获取 "wcout"来打印 unicode,并让 "cout"继续工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22950412/