是否可以将文本文件设置为UTF-16? [英] Is it possible to set a text file to UTF-16?

查看:90
本文介绍了是否可以将文本文件设置为UTF-16?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文本编写代码适用于ANSI字符,但是当我尝试编写日语字符时,它们不会出现.我需要使用UTF-16编码吗?如果是这样,我将如何在代码上做到这一点?

My code for writing text works for ANSI characters, but when I try to write Japanese characters they do not appear. Do I need to use UTF-16 encoding? If so, how would I do it on code?

std::wstring filename;
std::wstring text;
filename = "path";
wofstream myfile;
myfile.open(filename, ios::app);
getline(wcin, text);
myfile << text << endl;
wcin.get();
myfile.close();

推荐答案

从注释中看,您的控制台似乎正确理解了Unicode,而问题仅在于文件输出.

From the comments it seems your console correctly understands Unicode, and the issue is only with file output.

以下是在UTF-16LE中编写文本文件的方法.刚刚在MSVC 2019中进行了测试,并且可以正常工作.

Here's how to write a text file in UTF-16LE. Just tested in MSVC 2019 and it works.

#include <string>
#include <fstream>
#include <iostream>
#include <codecvt>
#include <locale>

int main() {
    std::wstring text = L"test тест 試験.";
    std::wofstream myfile("test.txt", std::ios::binary);
    std::locale loc(std::locale::classic(), new std::codecvt_utf16<wchar_t, 0x10ffff, std::little_endian>);
    myfile.imbue(loc);
    myfile << wchar_t(0xFEFF) /* UCS2-LE BOM */;
    myfile << text << "\n";
    myfile.close();
}

必须在Windows下使用 std :: ios :: binary 模式进行输出,否则 \ n 将通过扩展为 \ r \ n ,最终发出3个字节而不是2个字节.

You must use std::ios::binary mode for output under Windows, otherwise \n will break it by expanding to \r\n, ending up emitting 3 bytes instead of 2.

您不必在一开始就编写BOM,但是拥有一个BOM可以大大简化在文本编辑器中使用正确编码打开文件的过程.

You don't have to write the BOM at the beginning, but having one greatly simplifies opening the file using the correct encoding in text editors.

不幸的是,自C ++ 17起不推荐使用 std :: codecvt_utf16 (无替代)(是的,C ++对Unicode的支持很糟糕).

Unfortunately, std::codecvt_utf16 is deprecated since C++17 with no replacement (yes, Unicode support in C++ is that bad).

这篇关于是否可以将文本文件设置为UTF-16?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆