Writing comments to ini file with boost::property_tree::ptree

耗尽温柔 提交于 2019-12-19 03:17:05

问题


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

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 ?

Thanks in advance


回答1:


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

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!



来源:https://stackoverflow.com/questions/27354838/writing-comments-to-ini-file-with-boostproperty-treeptree

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!