带有口音的宽字符串未输出
wide strings with accents are not outputted
当我插入带有口音的字符串时,它不会显示在" fake.txt"(utf-16编码)
中std::wifstream ifFake("FAKE.txt", std::ios::binary);
ifFake.imbue(std::locale(ifFake.getloc(),
new std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>));
if (!ifFake)
{
std::wofstream ofFake("FAKE.txt", std::ios::binary);
ofFake << L"toc" << std::endl;
ofFake << L"salut" << std::endl;
ofFake << L"autre" << std::endl;
ofFake << L"êtres" << std::endl;
ofFake << L"âpres" << std::endl;
ofFake << L"bêtes" << std::endl;
}
结果(fake.txt)TOCsalut自动
其余的重音单词未写(我猜流错误)。
该程序是用G 编译的,源文件编码为UTF-8。
我注意到控制台输出相同的行为。
我该如何解决?
因为您没有imbue
ofFake
的语言环境。
下面的代码应很好地工作:
std::wofstream ofFake("FAKE.txt", std::ios::binary);
ofFake.imbue(std::locale(ofFake.getloc(),
new std::codecvt_utf16<wchar_t, 0x10ffff, std::generate_header>));
ofFake << std::wstring(L"toc") << std::endl;
ofFake << L"salut" << std::endl;
ofFake << L"autre" << std::endl;
ofFake << L"êtres" << std::endl;
ofFake << L"âpres" << std::endl;
ofFake << L"bêtes" << std::endl;
,尽管只有MSVC 二进制文件将制作UTF-16编码文件。G 二进制似乎像是用一些无用的BOM制作一个UTF8编码文件。
因此,我建议使用UTF8:
std::wofstream ofFake("FAKE.txt", std::ios::binary);
ofFake.imbue(std::locale(ofFake.getloc(), new std::codecvt_utf8<wchar_t>));
ofFake << L"toc" << std::endl;
ofFake << L"salut" << std::endl;
ofFake << L"autre" << std::endl;
ofFake << L"êtres" << std::endl;
ofFake << L"âpres" << std::endl;
ofFake << L"bêtes" << std::endl;
相关文章:
- 未获得字符串中的预期输出
- 由cin中的字符串中未捕获空白引起的分割错误
- C++ 命名参数习惯用语 - 未设置字符串属性
- 字符串变量未更新 c++
- 在 txt 文件中搜索中间子字符串未被选中的字符串
- 为什么SV后缀引入的字符串未过期
- 带有口音的宽字符串未输出
- php openssl 签名字符串未通过 Win CryptoAPI 验证
- 类固有虚拟字符串未返回正确的东西
- 字符串未返回到主函数
- "字符串"未命名类型 - C++ 错误
- 字符串未存储在向量中
- C++Visual Studio类错误标识符字符串未定义
- C++ Arduino 类文件中的字符串未编译
- 字符串未使用 std::cout 打印
- 字符串未使用 <openssl/aes>正确解密
- istringstream getline中的字符串未正确进行比较
- error:命名空间“std”中的“字符串”未命名类型 std::String isAble(long D, int T
- 字符串未在此范围C++中声明
- "字符串"未声明