property_tree组件可以设置自己想要的配置信息,这里就简单记录一下
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
int configure_sql_info(SqlConnInfo& sqlConnInfo) {
try {
boost::property_tree::ptree pt;
boost::property_tree::ini_parser::read_ini("http_conf.ini", pt);
sqlConnInfo.max_conn_num = pt.get<int>("SQL_IP_MASTER.max_conn_num");
sqlConnInfo.ip = pt.get< std::string>("SQL_IP_MASTER.ip");
sqlConnInfo.port = pt.get< std::string>("SQL_IP_MASTER.port");
sqlConnInfo.user_name = pt.get< std::string>("SQL_IP_MASTER.username");
sqlConnInfo.password = pt.get< std::string>("SQL_IP_MASTER.password");
} catch (...) {
std::cout << "except over ..." << std::endl;
}
return 0;
}
配置文件格式
http_conf.ini
[SQL_IP_MASTER]
max_conn_num = 4
ip= 192.168.11.227
port = 3306
username = T_ibaby
password = T_ibaby123