boost.property_tree读取中文乱码问题正确的解决方式
2024-08-22 13:51:34
开发项目的时候在使用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);
}
最新文章
- CSharpGL(2)设计和使用场景元素及常用接口
- 进阶——scrapy登录豆瓣解决cookie传递问题并爬取用户参加过的同城活动&#169;seven_clear
- ExcelReport第三篇:扩展元素格式化器
- android之apk反编译
- 【转】高性能Javascript--脚本的无阻塞加载策略
- POJ 1384
- bzoj3413
- NPOI 表头、页眉页脚重复设置
- Python字典(dict)使用技巧
- kafka监控项目大全
- mysql的基本操作笔记
- Linux 学习笔记 1
- hdu1695 GCD 容斥原理
- CentOS 7安装配置Samba服务器(挂载共享文件夹)
- PAT甲级 1130. Infix Expression (25)
- jquery获得下拉框值的代码
- Flex报错Error #2048: 安全沙箱冲突
- java练习:GUIformysql
- hammer使用: 代码:捏合、捏开、图片放大 的一个手机图片“放大缩小可拖动”的小效果
- 【Skynet】Traceback汇总
热门文章
- 我们一起学Docker(一)
- node-ejs-mongodb结合的项目案例-----引用mongoose和未引用mongoose模块
- ";fatal: protocol error: bad line length character: No This";
- DirectFB学习笔记一
- C语言学习第四章
- [移动端] IOS下border-image不起作用的解决办法
- ubuntu 使用第一天
- Hive-1.2.1与HBase-1.1.2的整合
- ShadowBroker释放的NSA工具中Esteemaudit漏洞复现过程
- Jdk1.6 JUC源码解析(1)-atomic-AtomicXXX