2014-11-21 3 views
0

부스트 라이브러리의 ini 파서 및 속성 트리를 사용하여 ini 파일을 쓰려고합니다. 파일은 단계적으로 작성됩니다. 즉, 모든 함수가 그 부분을 쓰는 것을 의미합니다. 끝에는 모든 것이 적어지지 않고 마지막 출력으로 남았습니다. 쓰는 동안 내가 사용C++ 부스트 라이브러리 - 덮어 쓰지 않고 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

그냥 단계에서 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 
+0

감사합니다! 좋은 간단한 솔루션 .. 다시 한 번 감사드립니다. – vito