首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >C++ Boost PropertyTree 解析INI文件

C++ Boost PropertyTree 解析INI文件

作者头像
王瑞MVP
发布于 2022-12-28 18:09:18
发布于 2022-12-28 18:09:18
1.1K0
举报

Boost PropertyTree 库不仅可以解析JSON,XML格式,还可以直接解析INI格式文件,并实现对文件的读写操作。

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-01-05,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档