c++ boost库读写ini配置文件
代码实例:
实现对.ini配置文件的读写的功能,使用前请先安装c++ boost库。
1.FileManagement.h
#ifndef _FILE_MANAGEMENT_H_
#define _FILE_MANAGEMENT_H_
#include <boost/property_tree/ini_parser.hpp>
#include <boost/exception/all.hpp>
#include <boost/filesystem.hpp> //-lboost_system -lboost_filesystem
#include <iostream>
#include <string>
namespace FileManagement
{
static std::string config_filename;
static bool read_ini_file(const std::string &filename, boost::property_tree::ptree &pt)
{
if(!boost::filesystem::exists(filename))
{
std::cerr << "File dose not exist:" << filename << std::endl;
return false;
}
config_filename = filename;
boost::property_tree::ini_parser::read_ini(filename, pt);
return true;
}
template <typename T>
bool get_ini_value(boost::property_tree::ptree &pt, const std::string &key, T &value)
{
try
{
value = pt.get<T>(key);
}
catch(std::exception e)
{
std::cout << config_filename << ":The " + key + " key does not exist!" << std::endl;
return false;
}
return true;
}
template <typename T>
void put_value(boost::property_tree::ptree &pt, const std::string &key, T &value)
{
pt.put(key, value);
}
static bool write_ini_file(boost::property_tree::ptree &pt,const std::string &filename)
{
try
{
boost::property_tree::ini_parser::write_ini(filename, pt);
}
catch(std::exception e)
{
std::cerr << e.what() << std::endl;
return false;
}
return true;
}
static bool saveConfig(const std::string &_filename, std::map<std::string, std::string> &_config_params)
{
boost::property_tree::ptree pt;
for(auto it : _config_params)
{
put_value(pt, it.first, it.second);
}
return write_ini_file(pt, _filename);
}
static bool readConfig(const std::string &_filename, std::map<std::string, std::string> &_config_params)
{
boost::property_tree::ptree pt;
bool ret = read_ini_file(_filename, pt);
if(ret)
{
for(auto &it : _config_params)
{
ret = get_ini_value(pt, it.first, it.second);
if(!ret)
{
break;
}
}
}
return ret;
}
}
#endif // _FILE_MANAGEMENT_H_
2.main.cpp
#include <FileManagement.h>
const std::string DB_CONFIG_FILENAME="./database.ini";
const std::string DB_CONFIG_BACK_FILENAME = "./database_back.ini";
int main()
{
std::map<std::string, std::string> config_params;
config_params["database.hostname"] = "";
config_params["database.port"] = "";
config_params["database.databaseName"] = "";
config_params["database.username"] = "";
config_params["database.password"] = "";
//Reading configuration
bool ret = FileManagement::readConfig(DB_CONFIG_FILENAME, config_params);
if(ret)
{
for(auto it : config_params)
{
std::cout << "key=" << it.first << " value=" << it.second << std::endl;
}
}
// Write to the configuration
FileManagement::saveConfig(DB_CONFIG_BACK_FILENAME, config_params);
return 0;
}
3.CMakeLists
cmake_minimum_required(VERSION 3.0.2)
project(read_config)
find_package(Boost REQUIRED COMPONENTS system filesystem thread)
include_directories(.)
add_executable(main main.cpp FileManagement.h)
target_link_libraries(main ${Boost_LIBRARIES})
4.database.ini
[database]
databaseName=user_info
hostname=127.0.0.1
password=147258
port=3306
username=root