读取ini配置

MiniINI是一个开源的,非常高效的,并且非常容易上手的INI库。使用C++实现的,同时也可以操作CFG文件。它本身不依赖除标准库以外的任何库文件,能够在支持C99的各种编译器上编译。采用MIT/X许可证,有兴趣的可以参考:https://code.launchpad.net/miniini。到现在为止,项目本身并不支持对INI文件的写操作,因此只有忍痛割爱了。它的接口非常简洁。可以看看示例代码(本身包含STL版本和非STL版本,示例是一个STL的版本示例)。可以看出函数的命名非常易懂,也很容易使用。

 

 

#include

#include

int main()
{
    INIFile ini;
    std::string fname = "example.ini";
    if(!ini.OpenFile(fname))
        std::cout << "ERROR: Could not open example.ini" << std::endl;

    std::string sname = "general";
    INISection * general = ini.GetSection(sname);
    if(!general)
        std::cout << "ERROR: Missing section [general] in example.ini"
                  << std::endl;

    std::string appname;
    int appbuild;
    float brightness;
    bool fullscreen;
    std::string appname_tag = "AppName";
    std::string appbuild_tag = "AppBuild";
    std::string brightness_tag = "Brightness";
    std::string fullscreen_tag = "FullScreen";
    if(!general->ReadString(appname_tag, appname))
        std::cout << "ERROR: Missing tag AppName= in section [" << sname 
                  << "] in example.ini" << std::endl;
    if(!general->ReadInt(appbuild_tag, appbuild))
        std::cout << "ERROR: Missing or invalid tag AppBuild= in section [" 
                  << sname << "] in example.ini" << std::endl;
    if(!general->ReadFloat(brightness_tag, brightness))
        std::cout << "ERROR: Missing or invalid tag Brightness= in section [" 
                  << sname << "] in example.ini" << std::endl;
    if(!general->ReadBool(fullscreen_tag, fullscreen))
        std::cout << "ERROR: Missing or invalid tag FullScreen= in section [" 
                  << sname << "] in example.ini" << std::endl;
    std::cout << appname << " build " << appbuild << " fullscreen is " 
              << fullscreen << " brightness is " << brightness << std::endl;

    return 0;
}


 

你可能感兴趣的:(C++语言)