Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ can't get "wcout" to print unicode, and leave "cout" working

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

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";

output:

abc hello world from cout!

i tried:

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

problem: "wcout" failed

tried:

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

and:

SetConsoleOutputCP(1251);

and

setlocale(LC_ALL, "");

and

SetConsoleCP(CP_UTF8)

Nothing worked

like image 430
user1438233 Avatar asked Apr 08 '14 23:04

user1438233


1 Answers

C++ says:

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

And the referenced document says:

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.

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

like image 56
Lightness Races in Orbit Avatar answered Sep 20 '22 11:09

Lightness Races in Orbit