user2953119
user2953119

Reputation:

Why does cout << "" work but wcout << "" not?

Why std::wcout << L"ي" << std::endl; doesn't print actual character?

#include <iostream>
#include <locale>

int main()
{
    std::locale::global (std::locale ("en_US.UTF-8"));
    std::cout << "ي" << std::endl;
    std::wcout << L"ي" << std::endl;
}

DEMO I define a suitable locale, but it still doesn't work

Upvotes: 2

Views: 1408

Answers (1)

Deduplicator
Deduplicator

Reputation: 45654

The error is mixing std::cout and std::wcout:

27.4 Standard iostream objects [iostream.objects]

27.4.1 Overview [iostream.objects.overview]

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.

7.21.2 Streams

4 Each stream has an orientation. After a stream is associated with an external file, but before any operations are performed on it, the stream is without orientation. Once a wide character input/output function has been applied to a stream without orientation, the stream becomes a wide-oriented stream. Similarly, once a byte input/output function has been applied to a stream without orientation, the stream becomes a byte-oriented stream. Only a call to the freopen function or the fwide function can otherwise alter the orientation of a stream. (A successful call to freopen removes any orientation.)267)
5 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. [...]

See here for a better demonstration highlighting what goes wrong and what does not:
http://coliru.stacked-crooked.com/a/5c3a34c0c4523016

Upvotes: 2

Related Questions