C++使用boost库读取ini配置文件的信息
2021/6/9 14:21:40
本文主要是介绍C++使用boost库读取ini配置文件的信息,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在启动程序或者服务器时,都要读取些配置文件,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文件的配置信息加载到定义的这些变量当中
这篇关于C++使用boost库读取ini配置文件的信息的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-28易优新手必读安装教程-icode9专业技术文章分享
- 2024-06-28忘记eyoucms后台密码怎么办?-icode9专业技术文章分享
- 2024-06-26终极指南:Scrum中如何设置需求优先级
- 2024-06-26AI大模型企业应用实战(25)-为Langchain Agent添加记忆功能
- 2024-06-26小白家庭 nas 搭建方案-icode9专业技术文章分享
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding
- 2024-06-23AI大模型企业应用实战(15)-langchain核心组件
- 2024-06-23AI大模型企业应用实战(16)-langchain核心组件
- 2024-06-23AI 大模型企业应用实战(06)-初识LangChain
- 2024-06-19EntBot.ai: AI Website Chatbot for Product Guides and Development Doc