首页 > 代码库 > boost读取utf-8编码xml文件

boost读取utf-8编码xml文件

参照:http://www.cnblogs.com/qicosmos/p/3555668.html

解决boost读取utf-8 xml文件乱码问题.

代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void io::read(const string& file)
{
    try
    {
        wifstream f(file);
        locale utf8Locale(locale("chs"), new codecvt_utf8<wchar_t>);
        f.imbue(utf8Locale);
        wptree pt;
        read_xml(f, pt);
        wstring w = pt.get<wstring>(L"class.name");
        string a(w.begin(),w.end());
        wcout.imbue(locale("chs"));
        wcout << w;
    }catch(exception e)
    {
        cout << "error: " << e.what();
    }
}

 2014-06-07 00:22:26