<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

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

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    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. }  
    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全