how to add config setting in libconfig for c++?

  c++, libconfig

I can get a libconfig::Config object by readFile method in libconfig, but if i want to add something into this config, how can i do?

here is my method:

#include <libconfig.h++>
#include <iostream>
using namespace std;

int main() {
  try{
    libconfig::Config cfg;
    cfg.readFile("/root/mmm/config/backtest/spread.config");
    libconfig::Setting & ss = cfg.getRoot().add("self_path", libconfig::Setting::TypeList);
    ss.add("huang", libconfig::Setting::TypeString);
    ss.add("xin", libconfig::Setting::TypeString);
    auto& array = cfg.lookup("self_path");
    for (int i = 0; i < array.getLength(); ++i) {
      std::string s = array[i];
      std::cout << s << std::endl;
    }
  } catch(const libconfig::SettingNotFoundException &nfex) {
    ERROR("Libconfig %s ERROR:Setting '%s' is missing", s, nfex.getPath());
    exit(1);
  } catch(const libconfig::SettingTypeException &tex) {
    ERROR("Libconfig %s ERROR:Setting '%s' has the wrong type", s, tex.getPath());
    exit(1);
  } catch (const std::exception& ex) {
    ERROR("Libconfig %s EXCEPTION: %sn", s, ex.what());
    exit(1);
  }
}

I want to add a list config setting into current cfg, but i got:

Setting 'self_path' has the wrong type

can you help on this?

Source: Windows Questions C++

LEAVE A COMMENT