简体   繁体   中英

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 ?

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::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!

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:

[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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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