C++ Boost PropertyTree 解析INI文件
2023-02-18 16:46:15 时间
Boost PropertyTree 库不仅可以解析JSON,XML格式,还可以直接解析INI格式文件,并实现对文件的读写操作。
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
using namespace std;
using namespace boost;
// 写入文件
void init_ini(const std::string &filename)
{
using boost::property_tree::ptree;
ptree pt;
// 增加新的键值对
pt.add("config.address", "192.168.1.1");
pt.add("config.port", 22);
// 修改原有键值对
pt.put("config.port", 3389);
write_ini(filename, pt);
}
int main(int argc, char *argv[])
{
std::string f("c://config.ini");
init_ini(f);
// 读取ini文件
boost::property_tree::ptree ptr, tag;
boost::property_tree::ini_parser::read_ini("c://config.ini", ptr);
tag = ptr.get_child("config");
std::string address = tag.get<std::string>("address");
int port = tag.get<int>("port");
std::cout << "地址: " << address << " 端口: " << port << std::endl;
std::system("pause");
return 0;
}
相关文章
- [TCP] 单台linux服务器最多支撑的tcp最大连接数
- [Go] redis分布式锁的go-redis实现
- [Go] 获取当前时间戳和时间戳单位转换
- [Go]字符串转int64数值型
- [Go] go下实现md5加密
- [Go] goland开启自动格式化和开启go modules
- [Git] git version 2以上git add .和git add -A 一样
- [Linux] 使用tcpdump查看上传文件过程中的tcp传输过程
- [Linux] 使用ifconfig查看tcp数据包的最大传输大小
- [Linux]利用tcpdump打印sql语句
- [Linux] 使用lsof命令查找进程打开的所有文件
- [Go] GO语言实战-slice的初始化影响转换成json后的数据结构
- [Go] GO语言实战-使用append函数要重新赋值
- [GOFLY在线客服系统] 开源版与商务版的区别
- [git] git stash 暂存未提交的修改文件
- [Go]Go语言实战-jwt-go会把存储的数值型转成float64
- [Go]GO语言实战-类型声明语句
- [linux] unzip把压缩文件解压到指定目录下 -d
- [git] git status命令查看文件状态
- [Go] gin框架中增加pprof分析性能