使用boost :: property_tree :: ptree将注释写入ini文件

Jef*_*eux 5 c++ ini boost

有没有办法使用boost :: property :: ptree在ini文件中写注释?

像这样的东西:

  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);
  }
Run Code Online (Sandbox Code Playgroud)

这里的文档没有提供信息.有没有提升工具呢?

提前致谢

小智 5

boost :: property_tree :: ptree用于各种"属性树"类型(INFO,INI,XML,JSON等),所以它本身不支持除了允许key => value的花哨容器之外的任何东西设置.你的最后一行(应该是):

boost::property_tree::ini_parser::write_ini(path, pt);
Run Code Online (Sandbox Code Playgroud)

是唯一能够定义你正在做什么的INI而不是其他格式的东西.例如,您可以通过写入XML轻松替换该行,它也可以工作.因此,您可以看到property_tree :: ptree不能具有特定类型文件的特定内容.


你能做的最好的事情就是为你的每个孩子添加一个"评论"设置 - 如下所示:

pt.put("something.comments", "Here are the comments for the 'something' section");
Run Code Online (Sandbox Code Playgroud)

您可以为任何具有任何名称的子项提供属性...在阅读期间迭代时,只需忽略它们.所以,如果你愿意,没有理由不像这样有创意 - 这是你的计划!

  • 我考虑过,但我想不加评论。我宁愿这样做,也不愿拥有包含注释的伪造属性。无论如何,谢谢! (3认同)