读书人

C++中如何读取UTF-8编码的文件

发布时间: 2012-03-04 11:13:33 作者: rapoo

C++中怎么读取UTF-8编码的文件?
求教各位大侠:

小弟要用C++读取一个UTF-8编码的文件,然后逐行打印到控制台。不知道该怎么做。
试过用宽字符解决,程序如下。但读出来的都是乱码。

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
wifstream winf;
winf.open( "test.txt ");

wstring wstr;
while(getline(winf, wstr))
{
wcout < < wstr < < endl;
}
winf.close();
return 0;
}

书上说wchar_t和wstring等类型是支持Unicode的,但我不知道它们支不支持UTF-8。另外我将原来的UTF-8文件在Notepad中转存成Unicode编码格式,用上面的程序读取,也还是乱码,而且乱码得比UTF-8文件还严重。不明白这是为什么?

请达人们指教下,读UTF-8文件用什么方法?标准C++对此有支持么?
谢谢!

[解决办法]
标准库中没有这个东西
需要自己将文件的内容转换格式
win下用 WideCharToMultiByte MultiByteToWideChar 转换编码格式
linux下用iconv转换编码格式.
[解决办法]
用C++的方法,需要自己写编码转换的方法,基本要吐血才能写好。
还是用这个简单些。
wchar_t linex[100];
FILE * f1;
f1=_wfopen(L "C:\\uni.txt ",L "rt+,ccs=UNICODE "); //or UTF-8
//int fileOpen=_wfopen_s(&f1,L "C:\\uni.txt ",L "rt+,ccs=UTF-8 ");

locale loc( " ");
wcout.imbue(loc);

while (!feof(f1))
{
fgetws(linex,100,f1);
wcout < <linex;
}
fclose(f1);

[解决办法]
inline void StringCodeChange(LPCTSTR _src, UINT _srcCode, string& _dest, UINT _destCode)
{
int len = MultiByteToWideChar(_srcCode , 0, _src, -1, NULL, 0);
WCHAR* _srcTemp = new WCHAR[len];
MultiByteToWideChar(_srcCode , 0, _src, -1, _srcTemp, len);
len = WideCharToMultiByte(_destCode, 0, _srcTemp, -1, NULL, 0, NULL, NULL);
char* _destTemp = new char[len];
WideCharToMultiByte(_destCode, 0, _srcTemp, -1, _destTemp, len, NULL, NULL);

_dest = _destTemp;

delete []_srcTemp;
delete []_destTemp;
}

char ss[256] = {(char)0xE8, (char)0x81, (char)0x8A, (char)0xE5, (char)0x9F, (char)0x8E,
(char)0xE7, (char)0xAB, (char)0x99, (char)0} ;
string s1, s2;
StringCodeChange(ss, CP_UTF8, s1, CP_ACP);
StringCodeChange(s1.c_str(), CP_ACP, s2, CP_UTF8);

读书人网 >C++

热点推荐