main.cpp
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/property_tree/ini_parser.hpp>
using namespace boost;
using namespace std;
//
class CIniFile
{
//
string m_path;
property_tree::ptree m_data;
public:
//
CIniFile(const string & filename)
{
m_path = exe_directory(filename);
property_tree::ini_parser::read_ini(m_path, m_data);
}
~CIniFile()
{
Write();
}
//
void Write(const string & path="")
{
string save_path = (path.length() ? exe_directory(path) : m_path);
property_tree::ini_parser::write_ini(save_path, m_data);
}
//
template <typename T>
T Get(const string & key)
{
return m_data.get<T>(key);
}
//
template <typename T>
void Set(const string & key, const T & value){
m_data.put(key, value);
}
};
//
CIniFile ini("test.ini");
cout << ini.Get<string>("app.f1") << endl;
ini.Set("app.f2", 12345);
ini.Write();