能否将文本文件设置为UTF-16编码?

4

我编写的文本代码可以正常处理ANSI字符,但尝试编写日文字符时未能正确显示。我需要使用UTF-16编码吗?如果需要,那该如何在代码中实现呢?

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();

2
那么,你是在终端中运行这个程序吗?是哪种终端?如果将具有不同字符集编码的文件导入到您的程序中会发生什么? - einpoklum
1
@JesperJuhl: 也许他只是在cmd会话中运行它,并且仅知道他正在键入日语。他期望wcin可以“正常工作”(这不是一个不合理的期望...) - einpoklum
1
你是否在调试器中检查过,在 getline(wcin, text) 之后,text 是否包含正确的字符? - rustyx
鉴于当前C++标准库对非ASCII字符集、区域设置和编码的处理状态,我实际上会说这是一个不合理的期望。 - Jesper Juhl
1
@rustyx 是的, text 包含了我键入的字符,在英语、日语和俄语中工作正常,只是文件本身不包含它们。 - Guilherme Galdino
显示剩余5条评论
3个回答

3

从评论中看来,您的控制台正确理解Unicode,问题只出现在文件输出上。

以下是如何在UTF-16LE编码下编写文本文件的方法。已在MSVC 2019中进行了测试并且有效。

#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个。

你不必在开头写BOM,但在文本编辑器中使用正确的编码打开文件时,有一个BOM可以大大简化操作。

不幸的是,自C++17以来std::codecvt_utf16已被弃用,并没有替代品(没错,C++中的Unicode支持就是那么差)。


这个问题确实问到了“我需要使用UTF-16编码吗?”,但对我来说似乎并不是强制要求使用UTF-16。我认为目标只是把Unicode保存到文件中。通常情况下,没有BOM的UTF-8会更好。 - Eryk Sun
立即生效!我仍然需要研究为什么它有效,但无论如何还是谢谢! - Guilherme Galdino
wcoutwofstream这样的宽字符流以宽字符(wchar_t)作为输入,并将它们输出为一系列字节。wchar_t如何被编码为字节取决于locale。默认情况下,使用一些单字节编码,例如cp1252,它最多可以表示256个代码点,其余的在编码过程中被简单地丢弃。codecvt_utf16 facet告诉localewchar_t编码为UTF-16。希望这能有所澄清... - rustyx

1

扩展我对你上一个问题的回答,这是一个用于编写文件的C库解决方案。我将源代码保存为UTF-8格式,并使用Microsoft的"cl /EHsc /W4 /utf-8 test.cpp"编译。

#include <fcntl.h>
#include <io.h>
#include <string>
#include <iostream>

// From fctrl.h:
//  #define _O_U16TEXT     0x20000 // file mode is UTF16 no BOM (translated)
//  #define _O_WTEXT       0x10000 // file mode is UTF16 (translated)

using namespace std;

int main()
{
    // Declare console I/O that works with Unicode.
    _setmode(_fileno(stdout),  _O_WTEXT);  // or _O_U16TEXT, either work
    _setmode(_fileno(stdin), _O_WTEXT);

    // Send a string to the console to verify stdout works with wide strings.
    wstring s = L"こんにちは, 世界!\nHello, World!";
    wcout << s << endl;

    // Read an input string.  I used an IME to enter Chinese.
    // Verify the stdin works...
    wstring test;
    getline(wcin, test);

    // Write it back out to stdout...
    wcout << test << endl;

    // Write it to a file as UTF-16.
    FILE *dest = fopen("out.txt", "w, ccs=UTF-16LE");
    fwprintf(dest, L"%s", test.c_str());
    return 0;
}

输出(控制台):

C:\>test
こんにちは, 世界!
Hello, World!
你好,马克!
你好,马克!

C:\>type out.txt
你好,马克!

文件内容的十六进制转储,显示UTF-16LE带BOM编码:
ff fe 60 4f 7d 59 0c ff 6c 9a 4b 51 01 ff

0

遗憾的是,在标准的C++中处理编码不是很方便。 在Posix系统上,单字节流效果更好,在Windows上,wchar_t流更方便。

为了处理文件编码,您需要使用imbuestd::locale设置到流中。

请注意,boost扩展了区域设置功能,因此可能会发现必须使用它才能使其正常工作。

通常建议使用系统区域设置:

// set global locale so wcin is able to read data properly
std::locale::global(std::locale{""});

std::wstring filename;
std::wstring text;
filename = "path";
wofstream myfile;

// if you need some specific text encoding check which one your system supports
// it may be something like "C.UTF-16"
myfile.imbue(std::locale{""});
myfile.open(filename, ios::app);
getline(wcin, text);
myfile << text << endl;
wcin.get();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接