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
- 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
- /usr/local/include/
Second, write the class file to read the parse INI
Ini.h
[CPP]View Plaincopy
- /*
- * file:ini.h
- * Author: [email protected]
- *
- * Created on March 18, 2013, 2:51
- */
- #ifndef Ini_h
- #define Ini_h
- #include <boost/property_tree/ptree.hpp>
- #include <boost/property_tree/ini_parser.hpp>
- #include <string>
- Using namespace std;
- Class ini{
- Public
- Ini (string ini_file);
- String get (string path);
- short int Errcode ();
- Private
- short int err_code;
- Boost::p roperty_tree::p tree m_pt;
- };
- #endif/* Ini_h */
Ini.cpp
[CPP]View Plaincopy
- #include "ini.h"
- Ini::ini (String ini_file) {
- if (Access (INI_FILE.C_STR (), 0) = = 0) {
- this->err_code = 0;
- Boost::p Roperty_tree::ini_parser::read_ini (Ini_file, this->m_pt);
- } Else {
- This->err_code = 1;
- }
- }
- Short Ini::errcode () {
- return this->err_code;
- }
- String Ini::get (string path) {
- if (This->err_code = = 0) {
- return this->m_pt.get<string> (path);
- } Else {
- return "";
- }
- }
Third, testing
Main.cpp
[CPP]View Plaincopy
- #include <cstdlib>
- #include <stdio.h>
- #include <iostream>
- #include <string>
- #include "ini.h"
- Using namespace std;
- /*
- *
- */
- int main (int argc, char** argv) {
- String ini_file = "/home/share/code/cppclass/test1.ini";
- INI ini (ini_file);
- Cout<<ini.get ("PUBLIC.ABC") <<endl;
- return 0;
- }