boost读写ini配置文件

[setting]
key1 = 1  
key2 = hello
上面是文件的内容(内容中不能有注释), 运行如下代码:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <iostream>

int main()
{
    boost::property_tree::ptree lvptProperties;
    boost::property_tree::ini_parser::read_ini("hao.txt", lvptProperties);
    boost::property_tree::basic_ptree<std::string, std::string> lvbtItems = lvptProperties.get_child("setting");

    for (auto lvitem=lvbtItems.begin();lvitem!=lvbtItems.end();lvitem++)
    {
        std::cout << (*lvitem).first.data() << "=" << (*lvitem).second.data() << std::endl;
    }
    int lvnInt = 0;
    try
    {
        lvnInt = lvbtItems.get<int>("key1");
        std::cout << lvnInt << std::endl;
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

    lvptProperties.put<std::string>("setting.key2", "new value");
    lvptProperties.put<std::string>("setting.key3", "new value");
    lvptProperties.put<int>("setting.key1", ++lvnInt);

    boost::property_tree::ini_parser::write_ini("hao.txt", lvptProperties);

    return 0;
}

运行结果:

key1=1
key2=hello
1

然后再重新打开hao.txt看内容:

--------------------- 
作者:OK_boom 
来源:CSDN 
原文:https://blog.csdn.net/rocklee/article/details/76021259 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值