我试图使用boost库的ini解析器和属性树来编写一个ini文件。 该文件是分阶段编写的 - 我的意思是每个函数都写入一部分。 最后,我只剩下最后一个输出而不是写下所有内容。
我在写作时使用的示例代码:
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);
如何在不覆盖文本其余部分的情况下使用write_ini函数?
答案 0 :(得分:0)
只需逐步构建ptree,并在完成后编写它:
<强> 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);
}
输出:
[AbbeyRoad]
startJunction=123890234,234898
endJunction=j340234,23495905
[Big Ben]
startJunction=123890234,234898
endJunction=j340234,23495905
[Trafalgar Square]
startJunction=123890234,234898
endJunction=j340234,23495905