用boost :: property_tree :: ptree将注释写入ini文件 [英] Writing comments to ini file with boost::property_tree::ptree

查看:133
本文介绍了用boost :: property_tree :: ptree将注释写入ini文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用boost :: property :: ptree在ini文件中写注释?

Is there any way to write comments in an ini file using boost::property::ptree ?

类似的东西:

  void save_ini(const std::string& path)
  {
      boost::property_tree::ptree pt;
      int first_value = 1;
      int second_value = 2;

      // Write a comment to describe what the first_value is here
      pt.put("something.first_value", );
      // Write a second comment here
      pt.put("something.second_value", second_value);

      boost::property_tree::write_ini(path, pt);
  }

文档此处不提供信息. Boost实现了吗?

The documentation here doesn't give the info. Did boost implement that ?

预先感谢

推荐答案

boost :: property_tree :: ptree用于多种属性树"类型(INFO,INI,XML,JSON等),因此除了作为允许key => value设置的高级容器外,它本身不支持任何其他功能.您的最后一行(应该是):

boost::property_tree::ptree is used for a variety of "property tree" types (INFO, INI, XML, JSON, etc.), so it does not inherently support anything other than being a fancy container for allowing key=>value settings. Your final line (which should be):

boost::property_tree::ini_parser::write_ini(path, pt);

是唯一定义您正在使用INI而不是其他格式之一的内容.例如,您可以轻松地用编写XML替换该行,它也可以工作.因此,您可以看到property_tree :: ptree不能具有特定于特定文件类型的内容.

is the only thing that is defining what you're doing as INI instead of one of those other formats. You could easily replace that line with writing to XML, for example, and it would also work. Therefore, you can see that the property_tree::ptree cannot have things specific to a specific type of file.


最好的办法是为每个孩子添加一个注释"设置-像这样:


The best you could do would be to add a "comments" setting for each of your children -- something like this:

pt.put("something.comments", "Here are the comments for the 'something' section");

您可以为任何具有任何所需名称的孩子设置属性...而在读取过程中进行遍历时,只需忽略它们即可.因此,如果您愿意,没有理由不像这样具有创造力-这是您的程序!

You can have properties for any child with any name you want...and simply ignore them when iterating through during read. So, there's no reason not to be creative like this if you wish -- it's your program!

这篇关于用boost :: property_tree :: ptree将注释写入ini文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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