How to use Libconfig

Asked 2 years ago, Updated 2 years ago, 28 views

I am writing a simple C++ app for Linux Debian.

I'm going to use Libconfig++ to read and write the config file, but I can read the config file, but I don't know how to update the contents.
https://hyperrealm.github.io/libconfig/libconfig_manual.html

The add method seems to add new settings and values, and although there is a writeFile, I don't know how to update the values.

Thank you for your cooperation.

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

# define CONFIG_FILE "sample_conf"

int main()
{
    Config cfg;
    try{
        cfg.readFile(CONFIG_FILE);
    }
    catch(const FileIOException & fioex){
        cerr<<"I/O error while reading file."<<endl;
    }
    catch(const ParseException&pex){
        cerr<<"Parse error at"<pex.getFile()<":"<pex.getLine()
            <<"-"<pex.getError()<endl;
        return(EXIT_FAILURE);
    }

    int start;
    intend;
    US>string file_name;

    try{
        if(!(cfg.lookupValue("start", start)))
            throw1;
        if(!(cfg.lookupValue("end", end)))
            throw2;
        if(!(cfg.lookupValue("file", file_name)))
            throw3;

        cout<<"Start"<<'\t'<<start<endl;
        cout<<"End"<<'\t'<<end<end;<end;end;<end;
        cout<<"File Name"<<'\t'<<file_name<<endl;
    }
    catch(intfError){
        cout<<"Itme Error"<<fError<<endl;
    }

}

sample_conf

start=10;
end = 20;
file="settings";

const Setting & root=cfg.getRoot();
const Setting & settings = root ["settings" ];

settings.lookupValue("start") = 30;

I saw it as , but I got an error.

Invalid arguments '
Candidates are:
boolean lookupValue(const char*, boolean&)
Boolean lookupValue(const char*, int&)
Boolean lookupValue(const char*, unsigned int&)
.
.

c++

2022-09-29 21:34

2 Answers

Method on Setting: Setting & operator=(bool value)
Method on Setting: Setting & operator=(int value)
Method on Setting: Setting & operator=(long value)
Method on Setting: Setting & operator=(const long & value)
Method on Setting: Setting & operator=(float value)
Method on Setting: Setting & operator=(const double & value)
Method on Setting: Setting & operator=(const char*value)
Method on Setting: Setting & operator=(const std::string & value)

Will you be using ?

cfg.lookup("start") = newValue;


2022-09-29 21:34

const Setting & root=getRoot();
root["start"] = 30;

I was able to solve this problem.Thank you.

This answer is a post of Comment of Questioner as a community wiki answer


2022-09-29 21:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.