提升读写ini文件

c_cpp
阅读 35 收藏 0 点赞 0 评论 0

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();
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号