让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(""));

你可能感兴趣的:(boost)