1

我有这个 .cfg 文件,我的问题是我正在尝试使用 .cfg 文件中的 libconfig 库读取和提取信息,但我无法创建 AboCombi 元素的根目录。我想知道如何创建超过 2 个层次结构元素的根。

我的配置文件

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);
}
4

0 回答 0