boost::property_tree读取解析ini文件

[cpp] view plaincopy
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <boost/property_tree/ptree.hpp>
  4. #include <boost/property_tree/ini_parser.hpp>
  5. int main()
  6. {
  7. boost::property_tree::ptree pt;
  8. boost::property_tree::ini_parser::read_ini("D:\\Overlay.ini", pt);
  9. std::cout << pt.get<std::string>("OVERLAY.OverlayFontName") << std::endl;
  10. pt.put<std::string>("OVERLAY.OverlayFontName","宋体");
  11. std::cout << pt.get<std::string>("OVERLAY.OverlayFontName") << std::endl;
  12. boost::property_tree::ini_parser::write_ini("D:\\Overlay.ini",pt);
  13. return 0;
  14. }

在C++11下,宽字节和单字节转换就简单了。使用std::wstring_convert和std::codecvt_utf8 来处理UTF8与WChar之间的互转.

[cpp] view plaincopy
  1. #include <iostream>
  2. #include <string>
  3. #include <locale>
  4. #include <codecvt>
  5. #include <fstream>
  6. int main(int argc, char *argv[])
  7. {
  8. std::wstring str = L"123,宋体!";
  9. std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
  10. std::string narrowStr = conv.to_bytes(str);
  11. {
  12. std::ofstream ofs ("c:\\test.txt");
  13. ofs << narrowStr;
  14. }
  15. std::wstring wideStr = conv.from_bytes(narrowStr);
  16. {
  17. std::locale::global(std::locale("Chinese-simplified"));
  18. std::wofstream ofs (L"c:\\testW.txt");
  19. ofs << wideStr;
  20. }

另外可以保存函数,使用ptree:

1 struct debug_simple
2 {
3     int itsNumber;
4     std::string itsName; //这里使用string就可以
5     void load(const std::string& filename); //载入函数
6     void save(const std::string& filename); //保存函数
7 };

保存函数,使用ptree:

 1 void debug_simple::save( const std::string& filename )2 {3     using boost::property_tree::ptree;4     ptree pt;5 6     pt.put("debug.number",itsNumber);7     pt.put("debug.name",itsName);8 9     write_xml(filename,pt);
10 }

载入函数使用的wptree,读取的值为wstring,需转换成string

 1 void debug_simple::load( const std::string& filename )2 {3     using boost::property_tree::wptree;4     wptree wpt;5     read_xml(filename, wpt);6 7     itsNumber = wpt.get<int>(L"debug.number");8     std::wstring wStr = wpt.get<std::wstring>(L"debug.name");9     itsName = std::string(wStr.begin(),wStr.end()); //wstring转string
10 }
main函数:
 1 int _tmain(int argc, _TCHAR* argv[])2 {3     4     try5     {6         debug_simple ds,read;7         ds.itsName = "汉字english";8         ds.itsNumber = 20;9
10         ds.save("simple.xml");
11         read.load("simple.xml");
12
13         std::cout<<read.itsNumber<<read.itsName;
14
15     }
16     catch (std::exception &e)
17     {
18         std::cout << "Error: " << e.what() << "\n";
19     }
20     return 0;
21 }

 

由于.ini文件是utf-8格式的,所以操作时要utf-8到unicode转换,或unicode到utf-8转换;

[cpp] view plaincopy
  1. // PropertyTree.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. using namespace std;
  6. #include <boost/property_tree/ptree.hpp>
  7. #include <boost/property_tree/ini_parser.hpp>
  8. //unicode 转UTF8
  9. //参数1是UTF8字符串当前位置指针,这里必须要是指针,因为必须要通过第1个字符进行判断才知道一个完整的字符的编码要向后取多少个字符
  10. //参数2是返回的UCS-2编码的Unicode字符
  11. inline int UTF82UnicodeOne(const char* utf8, wchar_t& wch)
  12. {
  13. if (utf8==NULL)
  14. {
  15. return -1;
  16. }
  17. //首字符的Ascii码大于0xC0才需要向后判断,否则,就肯定是单个ANSI字符了
  18. unsigned char firstCh = utf8[0];
  19. if (firstCh >= 0xC0)
  20. {
  21. //根据首字符的高位判断这是几个字母的UTF8编码
  22. int afters, code;
  23. if ((firstCh & 0xE0) == 0xC0)
  24. {
  25. afters = 2;
  26. code = firstCh & 0x1F;
  27. }
  28. else if ((firstCh & 0xF0) == 0xE0)
  29. {
  30. afters = 3;
  31. code = firstCh & 0xF;
  32. }
  33. else if ((firstCh & 0xF8) == 0xF0)
  34. {
  35. afters = 4;
  36. code = firstCh & 0x7;
  37. }
  38. else if ((firstCh & 0xFC) == 0xF8)
  39. {
  40. afters = 5;
  41. code = firstCh & 0x3;
  42. }
  43. else if ((firstCh & 0xFE) == 0xFC)
  44. {
  45. afters = 6;
  46. code = firstCh & 0x1;
  47. }
  48. else
  49. {
  50. wch = firstCh;
  51. return 1;
  52. }
  53. //知道了字节数量之后,还需要向后检查一下,如果检查失败,就简单的认为此UTF8编码有问题,或者不是UTF8编码,于是当成一个ANSI来返回处理
  54. for(int k = 1; k < afters; ++ k)
  55. {
  56. if ((utf8[k] & 0xC0) != 0x80)
  57. {
  58. //判断失败,不符合UTF8编码的规则,直接当成一个ANSI字符返回
  59. wch = firstCh;
  60. return 1;
  61. }
  62. code <<= 6;
  63. code |= (unsigned char)utf8[k] & 0x3F;
  64. }
  65. wch = code;
  66. return afters;
  67. }
  68. else
  69. {
  70. wch = firstCh;
  71. }
  72. return 1;
  73. }
  74. //参数1是UTF8编码的字符串
  75. //参数2是输出的UCS-2的Unicode字符串
  76. //参数3是参数1字符串的长度
  77. //使用的时候需要注意参数2所指向的内存块足够用。其实安全的办法是判断一下pUniBuf是否为NULL,如果为NULL则只统计输出长度不写pUniBuf,这样
  78. //通过两次函数调用就可以计算出实际所需要的Unicode缓存输出长度。当然,更简单的思路是:无论如何转换,UTF8的字符数量不可能比Unicode少,所
  79. //以可以简单的按照sizeof(wchar_t) * utf8Leng来分配pUniBuf的内存……
  80. int UTF82Unicode(const char* utf8Buf, wchar_t *pUniBuf, int utf8Leng)
  81. {
  82. if ((utf8Buf==NULL)||(pUniBuf==NULL))
  83. {
  84. return -1;
  85. }
  86. int i = 0, count = 0;
  87. while(i < utf8Leng)
  88. {
  89. i += UTF82UnicodeOne(utf8Buf + i, pUniBuf[count]);
  90. count ++;
  91. }
  92. return count;
  93. }
  94. inline int Unicode2UTF8One(unsigned wchar, char *utf8)
  95. {
  96. if (utf8==NULL)
  97. {
  98. return -1;
  99. }
  100. int len = 0;
  101. if (wchar < 0xC0)
  102. {
  103. utf8[len ++] = (char)wchar;
  104. }
  105. else if (wchar < 0x800)
  106. {
  107. utf8[len ++] = 0xc0 | (wchar >> 6);
  108. utf8[len ++] = 0x80 | (wchar & 0x3f);
  109. }
  110. else if (wchar < 0x10000)
  111. {
  112. utf8[len ++] = 0xe0 | (wchar >> 12);
  113. utf8[len ++] = 0x80 | ((wchar >> 6) & 0x3f);
  114. utf8[len ++] = 0x80 | (wchar & 0x3f);
  115. }
  116. else if (wchar < 0x200000)
  117. {
  118. utf8[len ++] = 0xf0 | ((int)wchar >> 18);
  119. utf8[len ++] = 0x80 | ((wchar >> 12) & 0x3f);
  120. utf8[len ++] = 0x80 | ((wchar >> 6) & 0x3f);
  121. utf8[len ++] = 0x80 | (wchar & 0x3f);
  122. }
  123. else if (wchar < 0x4000000)
  124. {
  125. utf8[len ++] = 0xf8 | ((int)wchar >> 24);
  126. utf8[len ++] = 0x80 | ((wchar >> 18) & 0x3f);
  127. utf8[len ++] = 0x80 | ((wchar >> 12) & 0x3f);
  128. utf8[len ++] = 0x80 | ((wchar >> 6) & 0x3f);
  129. utf8[len ++] = 0x80 | (wchar & 0x3f);
  130. }
  131. else if (wchar < 0x80000000)
  132. {
  133. utf8[len ++] = 0xfc | ((int)wchar >> 30);
  134. utf8[len ++] = 0x80 | ((wchar >> 24) & 0x3f);
  135. utf8[len ++] = 0x80 | ((wchar >> 18) & 0x3f);
  136. utf8[len ++] = 0x80 | ((wchar >> 12) & 0x3f);
  137. utf8[len ++] = 0x80 | ((wchar >> 6) & 0x3f);
  138. utf8[len ++] = 0x80 | (wchar & 0x3f);
  139. }
  140. return len;
  141. }
  142. //
  143. int Unicode2UTF8( const wchar_t *pUniBuf,char* utf8Buf, int UniLeng)
  144. {
  145. if ((utf8Buf==NULL)||(pUniBuf==NULL))
  146. {
  147. return -1;
  148. }
  149. int count = 0, i = 0;
  150. while(i < UniLeng)
  151. {
  152. count += Unicode2UTF8One(pUniBuf[i], utf8Buf+count);
  153. i ++;
  154. }
  155. return count;
  156. }
  157. int main()
  158. {
  159. boost::property_tree::ptree pt;
  160. using boost::property_tree::wptree;
  161. wptree wpt;
  162. boost::property_tree::ini_parser::read_ini("D:\\Overlay.ini", pt);
  163. std::string fontName=pt.get<std::string>("OVERLAY.OverlayFontName") ;
  164. wchar_t wfontName[128]={0};
  165. UTF82Unicode(fontName.c_str(),wfontName,fontName.length());
  166. std::wstring  wstrfontName=wfontName;
  167. //std::wcout << wstrfontName.c_str()<< std::endl;
  168. /*std::wstring */
  169. wstrfontName=_T("我是谁");
  170. char cfontName[128]={0};
  171. Unicode2UTF8(wstrfontName.c_str(),cfontName,wstrfontName.length());
  172. pt.put<std::string>("OVERLAY.OverlayFontName",cfontName);
  173. //std::cout << pt.get<std::string>("OVERLAY.OverlayFontName") << std::endl;
  174. boost::property_tree::ini_parser::write_ini("D:\\Overlay.ini",pt);
  175. return 0;
  176. }

boost::property_tree读取解析ini文件--推荐相关推荐

  1. 解决ini-parser解析ini文件中文乱码问题

    解决ini-parser解析ini文件中文乱码问题 参考文章: (1)解决ini-parser解析ini文件中文乱码问题 (2)https://www.cnblogs.com/nodegis/p/95 ...

  2. python解析ini文件

    新建一个config.ini文件 [LocalDB] ip=127.0.0.1 prot=3306 user=root passwd=123456 db=proxy python中解析ini文件的库为 ...

  3. vb 解析ini文件_PHP文件及运行(适合PHP初学者)

    PHP文件及运行(适合PHP初学者) PHP文件可包含HTML.JavaScript代码和 PHP代码,换句话说PHP 代码可以嵌入HTML文档.PHP文件名以php为后缀. PHP代码以" ...

  4. Python 内置模块之 ConfigParser - 解析 ini 文件

    ini配置文件是被configParser直接解析然后再加载的,如果只是修改配置文件,并不会改变已经加载的配置 INI文件结构简单描述 INI文件就是扩展名为"ini"的文件.在W ...

  5. Java当中解析ini文件对应到JavaBean当中

    目录 1.ini文件简介 2.ini文件 3.ini解析工具类 4.示例运行结果 1.ini文件简介 .ini 文件是Initialization File的缩写,即初始化文件,是windows的系统 ...

  6. API读取写入 ini文件内容的方法函数详解

    ini文件(即Initialization file),这种类型的文件中通常存放的是一个程序的初始化信息.ini文件由若干个节(Section)组成,每个Section由若干键(Key)组成,每个Ke ...

  7. Go语言读取解析yml文件,快速转换yml到go struct

    YAML (YAML Ain't a Markup Language)是一种标记语言,通常以.yml为后缀的文件,是一种直观的能够被计算机程序识别的数据序列化格式,并且容易被人类阅读,容易和脚本语言交 ...

  8. 如何运用JAXB定时读取解析xml文件?

    Background系统 一.背景 在许多开发需求中都解析xml文件的需求,对于规格复杂的xml文件,方法很多主要有JDK原生dom形式,SAX形式,DOM4J ,JAXB 4种方式,但是JAXB(J ...

  9. Java读取更新.ini文件(三)

    my.ini文件: [section] name=Konan [sect] name=zhangsan Java代码: package com.accord.util;import java.io.F ...

  10. boost::property_tree::ptree解析json数组

    json数组如下: {"var_name":"var1","positions":[0.1,0.1,0.1,0.1,0.1,0.1]},; ...

最新文章

  1. 【C 语言】文件操作 ( 配置文件读写 | 框架搭建 | 头文件定义 | 头文件导入限制 | 兼容 C++ 语言 | 函数形参输入输出属性注释)
  2. 国家卫计委倡导健康生活理念:每天发呆5分钟
  3. Vue钩子函数mounted实现进入页面立即查询的功能案例
  4. ACM与Java -- 大整数类的常用函数一览表
  5. python合并数组输出重复项_python进行数组合并的方法
  6. 帆软《商业智能》书籍首发,国产BI行业独家,福利发售!
  7. 统计字符数(信息学奥赛一本通-T1187)
  8. 基于Ogre的DeferredShading(延迟渲染)的实现以及应用
  9. 关于click事件在苹果手机上的阴影的解决方法和关于在安卓手机上的select的灰色背景色的解决...
  10. Ubuntu zip压缩文件夹 和解压文件
  11. NeatUpload使用方法
  12. Jeesit下面form:select的二级联动
  13. 腾讯视频国际版(Android)电量测试方法研究与总结
  14. 国内免费高匿IP代理软件
  15. psn账号修改地址可以转服务器,PSN换卡换服换账户教程
  16. apple configurator 2 获取appstore ipa包
  17. 大数据相加_大数据相加
  18. Java 对象排序完整版
  19. 电路板上为何要有孔洞?何谓PTH/NPTH/vias(导通孔)
  20. 上位机(地面站)之地图航线创建的摸索总结

热门文章

  1. SA: 情感分析资源(Corpus、Dictionary)
  2. 一些web开发中常用的、做成cs文件的js代码 - 转帖来的
  3. 你家的APS系统有这些功能吗?排程系统功能盘点
  4. 3.JAVA内存溢出
  5. 百度编辑器复制微信图片无法保存
  6. Eclipse没有Web插件和JavaEE插件咋整
  7. xdebug+webgrind
  8. docker镜像与容器概念
  9. 降低站长成本 推荐8个免费或低廉小型建站工具
  10. 一个敲有趣的R语言拼图工具