您的位置:首页 > 运维架构

让boost property_tree支持中文路径

2016-04-07 10:13 323 查看
让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