简体   繁体   English

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

[英]Writing comments to ini file with boost::property_tree::ptree

Is there any way to write comments in an ini file using boost::property::ptree ?有没有办法使用boost::property::ptree在 ini 文件中写注释?

Something like that:像这样的东西:

  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);
  }

The documentation here doesn't give the info. 这里的文档没有提供信息。 Did boost implement that? boost 实现了吗?

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. boost::property_tree::ptree 用于各种“属性树”类型(INFO、INI、XML、JSON 等),因此除了作为允许 key=>value 的精美容器外,它本身不支持任何东西设置。 Your final line (which should be):你的最后一行(应该是):

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

is the only thing that is defining what you're doing as INI instead of one of those other formats.是唯一将您正在做的事情定义为 INI 而不是其他格式之一的事情。 You could easily replace that line with writing to XML, for example, and it would also work.例如,您可以轻松地用写入 XML 来替换该行,它也可以工作。 Therefore, you can see that the property_tree::ptree cannot have things specific to a specific type of file.因此,您可以看到 property_tree::ptree 不能包含特定于特定类型文件的内容。


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!因此,如果您愿意,没有理由不进行这样的创意——这是您的程序!

using boost::property_tree::ptree;
ptree pt;
// first way
pt.put("a.;Below value is of float type", ">");
pt.put("a.value", 2.72f);**

//second way
pt.put("a.;The value of bvalue is used to show how to write comments in ini 
file:\nbvalue", 3.14f);
write_ini(filename, pt);

outputof .ini file: .ini 文件的输出:

[a]
;Below value is of float type=>
value=2.72000003
;The value of bvalue is used to show how to write comments in ini file:
bvalue=3.1400001

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM