How to read and write .ini files using boost library

How to read and write (or modify) .ini files using boost library?

+3
source share
1 answer

With the help of, Boost.PropertyTreeyou can read and update the tree, then write to the file (see functions loadand save.

See How to access data in the property tree . You can definitely add a new property or update an existing one. It mentions what is there eraseon the container so that you can delete the existing value. Example from boost(link above):

ptree pt;
pt.put("a.path.to.float.value", 3.14f);
// Overwrites the value
pt.put("a.path.to.float.value", 2.72f);
// Adds a second node with the new value.
pt.add("a.path.to.float.value", 3.14f);

, , .

: ini .

, ini ini_parser, :

  • ptree .

, ini , , :

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>

void save(const std::string &filename)
{
   using boost::property_tree::ptree;

//   pt.put("a.path.to.float.value", 3.14f);
//   pt.put("a.path.to.float.value", 2.72f);
//   pt.add("a.path.to.float.value", 3.14f);

   ptree pt;
   pt.put("a.value", 3.14f);
   // Overwrites the value
   pt.put("a.value", 2.72f);
   // Adds a second node with the new value.
   pt.add("a.bvalue", 3.14f);

   write_ini( filename, pt );
}

int main()
{
    std::string f( "test.ini" );
    save( f );
}

test.ini:

[a]
value=2.72
bvalue=3.14

.

+4

Source: https://habr.com/ru/post/1534806/


All Articles