C++ 中使用boost::property_tree读取解析ini文件

简介: boost 官网 http://www.boost.org/ 下载页面 http://sourceforge.net/projects/boost/files/boost/1.53.0/ 我下载的是 boost_1_53_0.

boost 官网 http://www.boost.org/

下载页面 http://sourceforge.net/projects/boost/files/boost/1.53.0/

我下载的是 boost_1_53_0.tar.gz

使用系统  ubuntu 12.10

 

一、解压

[plain]  view plain copy
 
  1. tar -zxvf  boost_1_53_0.tar.gz  

得到一个文件夹 boost_1_53_0,  拷贝其子目录 boost 到以下路径

[plain]  view plain copy
 
  1. /usr/local/include/  

 

二、编写读取解析ini的类文件

ini.h

[cpp]  view plain copy
 
  1. /* 
  2.  * File:   ini.h 
  3.  * Author: tsxw24@gmail.com 
  4.  * 
  5.  * Created on 2013年3月18日, 下午2:51 
  6.  */  
  7.   
  8. #ifndef INI_H  
  9. #define INI_H  
  10.   
  11.   
  12. #include <boost/property_tree/ptree.hpp>  
  13. #include <boost/property_tree/ini_parser.hpp>  
  14. #include <string>  
  15. using namespace std;  
  16.   
  17.   
  18. class Ini{  
  19. public:  
  20.     Ini(string ini_file);  
  21.     string get(string path);  
  22.     short int errCode();  
  23. private:  
  24.     short int err_code;  
  25.     boost::property_tree::ptree m_pt;  
  26. };  
  27.   
  28. #endif  /* INI_H */  

 

 

ini.cpp

[cpp]  view plain copy
 
  1. #include "ini.h"  
  2.   
  3.   
  4. Ini::Ini(string ini_file){  
  5.     if (access(ini_file.c_str(), 0) == 0) {  
  6.         this->err_code = 0;  
  7.         boost::property_tree::ini_parser::read_ini(ini_file, this->m_pt);  
  8.     } else {  
  9.         this->err_code = 1;  
  10.     }  
  11. }  
  12.   
  13. short Ini::errCode(){  
  14.     return this->err_code;  
  15. }  
  16.   
  17. string Ini::get(string path){  
  18.     if (this->err_code == 0) {  
  19.         return this->m_pt.get<string>(path);  
  20.     } else {  
  21.         return "";  
  22.     }  
  23. }  

 

 

 

三、测试

main.cpp

[cpp]  view plain copy
 
    1. #include <cstdlib>  
    2. #include <stdio.h>  
    3. #include <iostream>  
    4. #include <string>  
    5. #include "ini.h"  
    6.   
    7. using namespace std;  
    8.   
    9.   
    10.   
    11. /* 
    12.  * 
    13.  */  
    14. int main(int argc, char** argv) {  
    15.     string ini_file = "/home/share/code/CppClass/test1.ini";  
    16.     Ini ini(ini_file);  
    17.   
    18.     cout<<ini.get("public.abc")<<endl;  
    19.   
    20.   
    21.     return 0;  
    22. }  
目录
相关文章
|
7天前
|
C++ 数据格式
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
|
7天前
|
C++
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
|
7天前
|
Serverless C++ 容器
【期末不挂科-C++考前速过系列P5】大二C++实验作业-多态性(3道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P5】大二C++实验作业-多态性(3道代码题)【解析,注释】
|
7天前
|
C++ 芯片
【期末不挂科-C++考前速过系列P4】大二C++实验作业-继承和派生(3道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P4】大二C++实验作业-继承和派生(3道代码题)【解析,注释】
|
7天前
|
编译器 C++
【期末不挂科-C++考前速过系列P3】大二C++第3次过程考核(20道选择题&12道判断题&2道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P3】大二C++第3次过程考核(20道选择题&12道判断题&2道代码题)【解析,注释】
|
7天前
|
C++
【期末不挂科-C++考前速过系列P2】大二C++第2次过程考核(20道选择题&10道判断题&3道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P2】大二C++第2次过程考核(20道选择题&10道判断题&3道代码题)【解析,注释】
|
8天前
|
弹性计算 运维 Shell
|
23小时前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
9 0
|
23小时前
|
设计模式 算法 编译器
【C++入门到精通】特殊类的设计 |只能在堆 ( 栈 ) 上创建对象的类 |禁止拷贝和继承的类 [ C++入门 ]
【C++入门到精通】特殊类的设计 |只能在堆 ( 栈 ) 上创建对象的类 |禁止拷贝和继承的类 [ C++入门 ]
6 0
|
1天前
|
算法 安全 调度
【C++入门到精通】 线程库 | thread类 C++11 [ C++入门 ]
【C++入门到精通】 线程库 | thread类 C++11 [ C++入门 ]
8 1

推荐镜像

更多