让boost property_tree支持中文路径

让boost property_tree支持中文路径

#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>

using namespace std;

int main(int argc, char** argv) {
	string ini_file = "C:/工程1/test4.ini";
	std::locale::global(std::locale("")); //添加这句,可以让ini_file中包含中文字符,如果没有这句,会抛出异常,can not open file
	using boost::property_tree::ptree;
	ptree pt;
	pt.put("data.SourceImgPath", "a");
	pt.put("data.MaskImgPath", "b");
	pt.put("data.MarkedImgPath", "c");
	boost::property_tree::ini_parser::write_ini(ini_file, pt);
   
	return 0;
}






分析总结:
boost内部实际上用的是STL的串流 ,有时候用ifstream或ofstream打开带有中文路径的文件会失败。


解决办法:
1、使用C语言的函数设置为中文运行环境
setlocale(LC_ALL,"Chinese-simplified");


2、使用STL函数设置为系统语言环境
std::locale::global(std::locale(""));
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值