http://zh.cppreference.com/w/cpp/locale/codecvt_utf8
以前一直以为标准库的wofstream只能输出MBCS编码的文本文件,今日接触到codecvt后,知道自己完全错了。
研究一上午后,得出以下成果(均为自己读MSDN及实验得出)。注:以下代码及说明以VS2010为准。
先说codecvt头文件(gcc里没找到),这是MSDN的解释:http://msdn.microsoft.com/zh-cn/library/ee292114.aspx
里面包含了三个类:codecvt_utf8、codecvt_utf8_utf16、codecvt_utf16,以及一个枚举类型codecvt_mode。
codecvt是用于不同文字编码转换的一个类,codecvt_utfX继承了这个类,实现了不同编码转换的功能。
codecvt与locale结合使用,实现输出、读取UTF-8及UTF-16编码文本文件。
例如UTF-8:
#include <iostream>
#include <codecvt>
#include <fstream>
#include <string>
int main(void)
{
using namespace std;
auto LocUtf8=locale(locale(""),new codecvt_utf8<wchar_t>);
wofstream wfo(L"Hello.txt");
wfo.imbue(LocUtf8);
wfo << L"这是Utf-8编码的文本文件!";
wfo.close();
wifstream wfi(L"Hello.txt");
wstring wstr;
wfi.imbue(LocUtf8);
wfi >> wstr;
wcout.imbue(locale(""));
wcout << wstr << endl;
system("PAUSE");
}
UTF-8是比较特殊的编码格式,识别不需bom。如果将上例的utf8直接改为utf16后运行也有结果,但用记事本打开乱码,UTF-16需要bom头用来识别编码格式。如下:
#include <iostream>
#include <codecvt>
#include <fstream>
#include <string>
int main(void)
{
using namespace std;
auto LocUtf16=locale(locale(""),new codecvt_utf16<wchar_t, 1114111UL, generate_header>);
wofstream wfo(L"Hello.txt");
wfo.imbue(LocUtf16);
wfo << L"这是Utf-16编码的文本文件!";
wfo.close();
wifstream wfi(L"Hello.txt");
wstring wstr;
wfi.imbue(LocUtf16);
wfi >> wstr;
wcout.imbue(locale(""));
wcout << wstr << endl;
system("PAUSE");
}