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
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 onFILE
s, 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()
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.
By my interpretation this means, in short, do not mix std::cout
and std::wcout
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With