C ++ Boost库 - 写ini文件,而不会覆盖? [英] c++ boost library - writing to ini file without overwriting?

查看:583
本文介绍了C ++ Boost库 - 写ini文件,而不会覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写使用Boost库的INI解析器和属性树ini文件。
该文件是写在阶段 - 我的意思是每一个函数将它的一部分。
在结束IM只剩最后一个输出,而不是拥有一切写下来。

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.

样$ C C I使用而写:

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);

我怎么可以使用write_ini功能,但不覆盖文??

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

推荐答案

刚刚建立的步骤ptree中,写在做,只有当:

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

<大骨节病> 住在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

这篇关于C ++ Boost库 - 写ini文件,而不会覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆