gpt4 book ai didi

c++ - 为什么 cout << ""有效而 wcout << ""无效?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:42:30 29 4
gpt4 key购买 nike

为什么 std::wcout << L"ي" << std::endl;不打印实际字符?

#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 我定义了一个合适的语言环境,但它仍然不起作用

最佳答案

错误是混合 std::coutstd::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. [...]

请参阅此处以获得更好的演示,突出显示出了什么问题,什么没有:
http://coliru.stacked-crooked.com/a/5c3a34c0c4523016

关于c++ - 为什么 cout << ""有效而 wcout << ""无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26410497/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com