标签:str 配置文件 red cout oid 配置信息 figure 自带 port
在启动程序或者服务器时,都要读取些配置文件,windows有自带的WritePrivateProfileString可以将配置信息写入ini文件;但是跨平台的话,还是用boost的比较好。
如果配置文件名为test.ini,里面的配置信息如下:
[command]
host = 127.0.0.1
port = 7000
minsize = 2
maxsize = 10
timeout = 10
要将这些配置信息读出来,程序的实现如下:
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
void read_configure_ini()
{
boost::property_tree::ptree pt, commonCfg;
boost::property_tree::ini_parser::read_ini("./test.ini", pt);
string NodeIpStr;
int port;
int minsize;
int maxsize;
int timeout;
commonCfg= pt.get_child("command");
NodeIpStr = commonCfg.get<std::string>("host", "127.0.0.1");
port = commonCfg.get<int>("port", 7000);
minsize = commonCfg.get<int>("minsize", 2);
maxsize = commonCfg.get<int>("maxsize", 10);
timeout = commonCfg.get<int>("timeout", 10);
cout << "the ip is:" << redisNodeIpStr << endl;
cout << "the port is:" << port << endl;
cout << "the minsize is:" << minsize << endl;
cout << "the maxsize is:" << maxsize << endl;
cout << "the timeout is:" << timeout << endl;
}
int main()
{
read_configure_ini();
return 0;
}
这样的话就能将读取到的ini文件的配置信息加载到定义的这些变量当中
标签:str 配置文件 red cout oid 配置信息 figure 自带 port
原文地址:https://www.cnblogs.com/Unclebigdata/p/14866604.html