DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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 plaincopy
 
  1. tar -zxvf  boost_1_53_0.tar.gz  

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

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

 

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

ini.h

[cpp] view plaincopy
 
  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 plaincopy
 
  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 plaincopy
 
    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. }  
posted on   DoubleLi  阅读(3063)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示