Boost Ini-parser: preserve comments in ini-file on write

Viewed 1437

I am trying to parse an Ini-file (using boost) and write some changed values back to the file. A simple example for this is the following code (main.cpp):

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

int main()
{
    try
    {
        boost::property_tree::ptree pt;
        boost::property_tree::ini_parser::read_ini("testini.ini", pt);

        int i = pt.get<int>("Section1.Value1");

        std::cout << i << std::endl;

        pt.put("Section1.Value1", 32);

        boost::property_tree::ini_parser::write_ini( "testini.ini", pt );
    }
    catch (std::exception& e)
    {
        std::cout << e.what() << std::endl;
    } 
    return 0;
}

With this, I am accessing the following ini file (testini.ini):

[Section1]
; Test variable
Value1=1

This works quite well, however, the comment is not preserved and the newly written file looks like this

[Section1]
Value1=32

and what I wanted is

[Section1]
; Test variable
Value1=32

Now the question is, is there a way to preserve the comment in the file? It must not need to use the ini-parser!

0 Answers
Related