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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 04:00:05

问题


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??


回答1:


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


来源:https://stackoverflow.com/questions/27067475/c-boost-library-writing-to-ini-file-without-overwriting

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