简体   繁体   中英

c++ boost library - writing to ini file without overwriting?

im trying to write an ini file using boost library's ini parser and property tree. The file is written in stages - i mean every function writes a portion of it. At the end im left with only the last output instead of having everything written down.

Sample code i use while writing:

property_tree::ptree pt;
string juncs=roadID;
size_t pos = juncs.find_last_of("j");
string jstart = juncs.substr(0,pos);
string jend = juncs.substr(pos,juncs.length());
pt.add(repID + ".startJunction", jstart);
pt.add(repID + ".endJunction", jend);
write_ini("Report.ini", pt);

How can i use the write_ini function without overwriting the rest of the text??

Just build the ptree in steps, and write it only when done:

Live On Coliru

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

using namespace boost::property_tree;

struct X {
    void add_junction(std::string repID, ptree& pt) const {
        std::string juncs  = _roadID;
        std::size_t pos    = juncs.find_last_of("j");
        std::string jstart = juncs.substr(0,pos);
        std::string jend   = juncs.substr(pos,juncs.length());

        pt.add(repID + ".startJunction", jstart);
        pt.add(repID + ".endJunction", jend);
    }

    std::string _roadID = "123890234,234898j340234,23495905";
};

int main()
{
    ptree pt;

    X program_data;
    program_data.add_junction("AbbeyRoad", pt);
    program_data.add_junction("Big Ben", pt);
    program_data.add_junction("Trafalgar Square", pt);

    write_ini("report.ini", pt);
}

Output:

[AbbeyRoad]
startJunction=123890234,234898
endJunction=j340234,23495905
[Big Ben]
startJunction=123890234,234898
endJunction=j340234,23495905
[Trafalgar Square]
startJunction=123890234,234898
endJunction=j340234,23495905

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