c ++无法获得"wcout"来打印unicode,并留下"cout"在职的 [英] c++ can't get "wcout" to print unicode, and leave "cout" working

查看:71
本文介绍了c ++无法获得"wcout"来打印unicode,并留下"cout"在职的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法获得"wcout"在多个代码页中打印unicode字符串,并留下"cout"工作

can't get "wcout" to print unicode string in multiple code pages, together with leaving "cout" to work

请帮助我让这3条线一起工作.

please help me get these 3 lines to work together.

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"失败

problem: "wcout" failed

尝试:

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

和:

SetConsoleOutputCP(1251);

setlocale(LC_ALL, "");

SetConsoleCP(CP_UTF8)

什么都没做

推荐答案

C ++说:

[C ++ 11:27.4.1/3]:在相应的宽字符流和窄字符流上的混合操作遵循与在 FILE s,如ISO C标准的修订1所述.

[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.

引用的文档说:

更改了流的定义,以包括文本流和二进制流的方向概念.在将流与文件关联之后,但是在对该流执行任何操作之前,该流没有方向.如果将宽字符输入或输出功能应用于没有方向的流,则该流将变为宽方向.同样,如果将字节输入或输出操作应用于具有方向的流,则该流将变为面向字节的.此后,只有 fwide() freopen()函数可以更改流的方向.

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.

字节输入/输出功能不得应用于宽方向的流,宽字符输入/输出功能不应适用于面向字节的流.

根据我的解释,简而言之,请勿混用 std :: cout std :: wcout .

By my interpretation this means, in short, do not mix std::cout and std::wcout.

这篇关于c ++无法获得"wcout"来打印unicode,并留下"cout"在职的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆