Use boost in C + +::p roperty_tree Read parse INI file

Source: Internet
Author: User

Boost official website http://www.boost.org/

Download Page http://sourceforge.net/projects/boost/files/boost/1.53.0/

I downloaded the boost_1_53_0.tar.gz.

Using the system Ubuntu 12.10

First, decompression

[Plain]View Plaincopy
    1. TAR-ZXVF boost_1_53_0.tar.gz

Get a folder Boost_1_53_0, copy its sub-directories boost to the following path

[Plain]View Plaincopy
    1. /usr/local/include/

Second, write the class file to read the parse INI

Ini.h

[CPP]View Plaincopy
  1. /*
  2. * file:ini.h
  3. * Author: [email protected]
  4. *
  5. * Created on March 18, 2013, 2:51
  6. */
  7. #ifndef Ini_h
  8. #define Ini_h
  9. #include <boost/property_tree/ptree.hpp>
  10. #include <boost/property_tree/ini_parser.hpp>
  11. #include <string>
  12. Using namespace std;
  13. Class ini{
  14. Public
  15. Ini (string ini_file);
  16. String get (string path);
  17. short int Errcode ();
  18. Private
  19. short int err_code;
  20. Boost::p roperty_tree::p tree m_pt;
  21. };
  22. #endif/* Ini_h */

Ini.cpp

[CPP]View Plaincopy
  1. #include "ini.h"
  2. Ini::ini (String ini_file) {
  3. if (Access (INI_FILE.C_STR (), 0) = = 0) {
  4. this->err_code = 0;
  5. Boost::p Roperty_tree::ini_parser::read_ini (Ini_file, this->m_pt);
  6. } Else {
  7. This->err_code = 1;
  8. }
  9. }
  10. Short Ini::errcode () {
  11. return this->err_code;
  12. }
  13. String Ini::get (string path) {
  14. if (This->err_code = = 0) {
  15. return this->m_pt.get<string> (path);
  16. } Else {
  17. return "";
  18. }
  19. }

Third, testing

Main.cpp

[CPP]View Plaincopy
    1. #include <cstdlib>
    2. #include <stdio.h>
    3. #include <iostream>
    4. #include <string>
    5. #include "ini.h"
    6. Using namespace std;
    7. /*
    8. *
    9. */
    10. int main (int argc, char** argv) {
    11. String ini_file = "/home/share/code/cppclass/test1.ini";
    12. INI ini (ini_file);
    13. Cout<<ini.get ("PUBLIC.ABC") <<endl;
    14. return 0;
    15. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.