开发项目的时候在使用boost,在宽字符下遇到中文乱码问题

上网上看大家都是先转成utf8在进行解析的,例如:

http://blog.csdn.net/hu_jiangan/article/details/49945373 中

void Init(const wstring& fileName, wptree& ptree)
{
std::wifstream f(fileName);
std::locale utf8Locale(std::locale(), new std::codecvt_utf8<wchar_t>);
f.imbue(utf8Locale); //先转换一下
//用wptree去解析
property_tree::read_xml(f, ptree);
}

  他的思路没有问题,并且还用了STL库,避免了boost的繁琐。

但是在boost中对read_xml定义里包含了对xml的转换

 template<class Ptree>
void read_xml(const std::string &filename,
Ptree &pt,
int flags = ,
const std::locale &loc = std::locale())
{
BOOST_ASSERT(validate_flags(flags));
std::basic_ifstream<typename Ptree::key_type::value_type>
stream(filename.c_str());
if (!stream)
BOOST_PROPERTY_TREE_THROW(xml_parser_error(
"cannot open file", filename, ));
stream.imbue(loc);
read_xml_internal(stream, pt, flags, filename);
}

所以他对xml进行了两次utf8的转换
正确的代码其实只需要两行就可以完成

void Init(const wstring& fileName, wptree& ptree)
{
std::locale utf8Locale(std::locale(), new std::codecvt_utf8<wchar_t>);
boost::property_tree::read_xml(filename, ptree, , utf8Locale);
}

最新文章

  1. CSharpGL(2)设计和使用场景元素及常用接口
  2. 进阶——scrapy登录豆瓣解决cookie传递问题并爬取用户参加过的同城活动&#169;seven_clear
  3. ExcelReport第三篇:扩展元素格式化器
  4. android之apk反编译
  5. 【转】高性能Javascript--脚本的无阻塞加载策略
  6. POJ 1384
  7. bzoj3413
  8. NPOI 表头、页眉页脚重复设置
  9. Python字典(dict)使用技巧
  10. kafka监控项目大全
  11. mysql的基本操作笔记
  12. Linux 学习笔记 1
  13. hdu1695 GCD 容斥原理
  14. CentOS 7安装配置Samba服务器(挂载共享文件夹)
  15. PAT甲级 1130. Infix Expression (25)
  16. jquery获得下拉框值的代码
  17. Flex报错Error #2048: 安全沙箱冲突
  18. java练习:GUIformysql
  19. hammer使用: 代码:捏合、捏开、图片放大 的一个手机图片“放大缩小可拖动”的小效果
  20. 【Skynet】Traceback汇总

热门文章

  1. 我们一起学Docker(一)
  2. node-ejs-mongodb结合的项目案例-----引用mongoose和未引用mongoose模块
  3. &quot;fatal: protocol error: bad line length character: No This&quot;
  4. DirectFB学习笔记一
  5. C语言学习第四章
  6. [移动端] IOS下border-image不起作用的解决办法
  7. ubuntu 使用第一天
  8. Hive-1.2.1与HBase-1.1.2的整合
  9. ShadowBroker释放的NSA工具中Esteemaudit漏洞复现过程
  10. Jdk1.6 JUC源码解析(1)-atomic-AtomicXXX