C++ how to make a root of .cfg file

135 views Asked by At

I have this .cfg file and my problem is that I am trying to read and extract info using the libconfig library in the .cfg file but I can't create a root up to the AboCombi element. I would like to know how to create a root of more than 2 elements of a hierarchy.

myConfig.cfg

clients =
{
  name = "Robert";
  client_infos = (
                {
                    town = "Koeln";
                    Start_abo = "09.12.2020";
                    AboCombi = (
                        {
                            Courses = "All";
                            Materials = "ADTF_For_Free";
                            Matric = "A0003";

                        },
                    );

                },
};

    int main(int argc, char** argv) {
    (void)argc;
    (void)argv;

    Config cfg;

    // Read the file. If there is an error, report it and exit.
    try
    {
        cfg.readFile("myConfig.cfg");
    }
    catch (const FileIOException &fioex)
    {
        cerr << "I/O error while reading file." << endl;
        return(EXIT_FAILURE);
    }
    catch (const ParseException& pex)
    {
        cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
            << " - " << pex.getError() << endl;
        return(EXIT_FAILURE);
    }

    const Setting& root = cfg.getRoot();

    // Output a list of the main config file.
    try
    {
        const Setting &croot = root["clients"]["client_infos"]; // but I would like to go further down the hierarchy to AboCombi and its elements 
        int count = croot.getLength();

        cout << count << endl;  

    }
    catch (const SettingNotFoundException &nfex)
    {
        // Ignore.
    }

    return(EXIT_SUCCESS);
}
0

There are 0 answers