c++ - libconfig++ : adding a setting to the root of the configuration tree -
i have configuration file mycfg.cfg
looks :
keya = 1.0 keyb = 2 keyc = "hello"
note settings @ root of configuration tree.
i want c++ program load file add new setting key keyd
, assign integer value 5
. eventually, mycfg
should in memory :
keya = 1.0 keyb = 2 keyc = "hello" keyd = 5
first mycfg.cfg
file loaded construct mycfg
. setting keyd
must added root of mycfg
. libconfig documentation indicates setting::add()
method :
add[s] new child setting given name , type setting, must group
however, there isn't group in mycfg
… how add setting root of config object ?
it looks need is: getroot ()
.
here example:
#include <iostream> #include "libconfig.h++" int main () { libconfig::config mycfg; std::string file = "mycfg.cfg"; try { mycfg.readfile (file.c_str () ); libconfig::setting & root = mycfg.getroot (); // works. // libconfig::setting & root = mycfg.lookup (""); libconfig::setting & keyd = root.add ("keyd", libconfig::setting::typeint); keyd = 5; // dont need it, it's testing. mycfg.writefile (file.c_str () ); } catch (...) { std::cout << "error caused!" << std::endl; } return 0; }
Comments
Post a Comment