zoukankan      html  css  js  c++  java
  • C++ 中使用boost::property_tree读取解析ini文件

    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. }  
  • 相关阅读:
    Android stadio 插件推荐--ok gradle
    算法:枚举法---kotlin
    Kotlin 二分法算法游戏--猜价格
    android onCreate的两个方法
    Spring 中的scope
    Intellij IDEA 2017 debug断点调试技巧与总结详解篇
    深入浅出ConcurrentHashMap1.8
    ConcurrentHashMap JDK1.8
    synchronized修饰普通方法和静态方法
    Java多线程系列--CopyOnWriteArraySet
  • 原文地址:https://www.cnblogs.com/lidabo/p/3905824.html
Copyright © 2011-2022 走看看