问题
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