boost.property_tree读取中文乱码问题

最近突然怀念这个ID了,就找回来了,以后陆续把其他博客文章整理回来

boost库很好用,不过有些库比较新,用起来磕磕碰碰的,比如这个boost.property_tree默认情况下对宽字符支持不怎么友好

搜了一下,经过几次尝试,找到了好的方案,需要先做一次本地化编码转换:

#include "boost/program_options/detail/utf8_codecvt_facet.hpp"
 
int test()
{
    std::wifstream f(L"测试.xml");
    std::locale utf8Locale(std::locale(),new boost::program_options::detail::utf8_codecvt_facet());
    f.imbue(utf8Locale);
     
    if(f)
    {
        boost::property_tree::read_xml(f,pt);
        std::wstring text = pt.get(L"test",L"");
        MessageBox(NULL,text.c_str(),L"",MB_OK);
    }
}
原文地址:https://www.cnblogs.com/wuyaSama/p/2470284.html