如何阅读和使用Boost库写入.ini文件 [英] How to read and write .ini files using boost library

查看:1901
本文介绍了如何阅读和使用Boost库写入.ini文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何使用boost库来读取和写入(或修改).ini文件。是否有任何人能告诉我该怎么办呢?

I need to know how to read and write (or modify) .ini files using boost library. Does any one can tell me how to do it?

推荐答案

通过 Boost.PropertyTree 您可以读取和更新树,然后写一个文件(请参阅负荷保存功能。

With Boost.PropertyTree you can read and update the tree, then write to a file (see load and save functions.

看一看如何属性访问数据树
你绝对可以添加新的属性或更新现有之一。
它mentiones存在的删除集装箱以及所以你应该能够删除现有的值。从升压示例(以上链接):

Have a look at How to access data in property tree. You can definitely add new property or update existing one. It mentiones that there's erase on container as well so you should be able to delete existing value. Example from boost (link above):

ptree pt;
pt.put("a.path.to.float.value", 3.14f);
// Overwrites the value
pt.put("a.path.to.float.value", 2.72f);
// Adds a second node with the new value.
pt.add("a.path.to.float.value", 3.14f);

我会假设,那么你会写更新树到一个文件中,无论是新的或覆盖现有之一。

I would assume you would then write updated tree into a file, either new one or overwrite existing one.

编辑:
对于ini文件确实专项检查。

EDIT : For ini file it does specific checks.

上面的例子,如果你试图保存你ini_parser到INI:

The above example if you try to save to ini with ini_parser you get:


  1. ptree中太深

  2. 重复键

通过这里定是一个例子code写入ini文件,我已经更新现有键的值则增加了一个新的关键:

With that fixed here is an example code that writes ini file, I've updated a value of existing key then added a new key:

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

void save(const std::string &filename)
{
   using boost::property_tree::ptree;

//   pt.put("a.path.to.float.value", 3.14f);
//   pt.put("a.path.to.float.value", 2.72f);
//   pt.add("a.path.to.float.value", 3.14f);

   ptree pt;
   pt.put("a.value", 3.14f);
   // Overwrites the value
   pt.put("a.value", 2.72f);
   // Adds a second node with the new value.
   pt.add("a.bvalue", 3.14f);

   write_ini( filename, pt );
}

int main()
{
    std::string f( "test.ini" );
    save( f );
}

test.ini 文件:

[a]
value=2.72
bvalue=3.14

随时实验。

这篇关于如何阅读和使用Boost库写入.ini文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆