c++ boost库读写ini配置文件

c++ boost库读写ini配置文件

代码实例:

实现对.ini配置文件的读写的功能,使用前请先安装c++ boost库。

1.FileManagement.h

#ifndef _FILE_MANAGEMENT_H_
#define _FILE_MANAGEMENT_H_

#include <boost/property_tree/ini_parser.hpp>
#include <boost/exception/all.hpp>
#include <boost/filesystem.hpp> //-lboost_system -lboost_filesystem
#include <iostream>
#include <string>

namespace FileManagement
{
    static std::string config_filename;
    static bool read_ini_file(const std::string &filename, boost::property_tree::ptree &pt)
    {
        if(!boost::filesystem::exists(filename))
        {
            std::cerr << "File dose not exist:" << filename << std::endl;
            return false;
        }
        config_filename = filename;
        boost::property_tree::ini_parser::read_ini(filename, pt);

        return true;
    }

    template <typename T>
    bool get_ini_value(boost::property_tree::ptree &pt, const std::string &key, T &value)
    {
        try
        {
            value = pt.get<T>(key);
        }
        catch(std::exception e)
        {
            std::cout << config_filename << ":The " + key + " key does not exist!" << std::endl;
            return false;
        }
        return true;
    }

    template <typename T>
    void put_value(boost::property_tree::ptree &pt, const std::string &key, T &value)
    {
        pt.put(key, value);
    }

    static bool write_ini_file(boost::property_tree::ptree &pt,const std::string &filename)
    {
        try
        {
            boost::property_tree::ini_parser::write_ini(filename, pt);
        }
        catch(std::exception e)
        {
            std::cerr << e.what() << std::endl;
            return false;
        }

        return true;
    }

    static bool saveConfig(const std::string &_filename, std::map<std::string, std::string> &_config_params)
    {
        boost::property_tree::ptree pt;
        for(auto it : _config_params)
        {
            put_value(pt, it.first, it.second);
        }
        return write_ini_file(pt, _filename);
    }

    static bool readConfig(const std::string &_filename, std::map<std::string, std::string> &_config_params)
    {
        boost::property_tree::ptree pt;
        bool ret = read_ini_file(_filename, pt);
        if(ret)
        {
            for(auto &it : _config_params)
            {
                ret = get_ini_value(pt, it.first, it.second);
                if(!ret)
                {
                   break;
                }
            }
        }
        return ret;
    }
}


#endif // _FILE_MANAGEMENT_H_

2.main.cpp

#include <FileManagement.h>

const std::string DB_CONFIG_FILENAME="./database.ini";
const std::string DB_CONFIG_BACK_FILENAME = "./database_back.ini"; 

int main()
{
    std::map<std::string, std::string> config_params;
    config_params["database.hostname"] = "";
    config_params["database.port"] = "";
    config_params["database.databaseName"] = "";
    config_params["database.username"] = "";
    config_params["database.password"] = "";

    //Reading configuration
    bool ret = FileManagement::readConfig(DB_CONFIG_FILENAME, config_params);
    if(ret)
    {
        for(auto it : config_params)
        {
            std::cout << "key=" << it.first << " value=" << it.second << std::endl;
        }
    }

    // Write to the configuration
    FileManagement::saveConfig(DB_CONFIG_BACK_FILENAME, config_params);

    return 0;
}

3.CMakeLists

cmake_minimum_required(VERSION 3.0.2)
project(read_config)

find_package(Boost REQUIRED COMPONENTS system filesystem thread)
include_directories(.)

add_executable(main main.cpp FileManagement.h)
target_link_libraries(main ${Boost_LIBRARIES})

4.database.ini

[database]
databaseName=user_info
hostname=127.0.0.1
password=147258
port=3306
username=root

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: C标准本身并没有提供直接读写ini文件的函数,但可以通过结合其他或自行实现一套读写ini文件的功能。 一种常用的方法是使用libconfig进行ini文件的读写操作。该提供了对ini文件的解析和生成的函数,使用简单方便。首先需要在代码中引入libconfig头文件,然后通过函数调用实现ini文件的读取和写入。 在读取ini文件时,可以通过调用libconfig提供的函数逐个获取ini文件中的section和key的值,并进行相应的逻辑处理。 在写入ini文件时,首先需要创建一个用于存储ini文件数据的config_t对象。然后可以使用函数逐个添加section和key的值,并最后通过函数将数据写入ini文件。 除了使用libconfig外,也可以自行实现读写ini文件的功能。这通常涉及到文件的打开、读取、写入和关闭等操作。在读取ini文件时,可以通过使用C标准提供的文件读取函数,逐行读取ini文件内容并解析,获取需要的section和key以及其对应的值。在写入ini文件时,可以使用C标准提供的文件写入函数,将数据按照ini文件的格式写入到文件中。 无论是使用libconfig还是自行实现读写ini文件的功能,都需要注意处理文件不存在、文件格式错误以及数据读写错误等异常情况。同时,还需要注意保证对文件的读写操作是线程安全的,以及对ini文件中可能出现的特殊字符和编码格式进行正确的处理。 ### 回答2: C标准本身并不提供直接读写INI文件的功能,但我们可以利用C标准中的一些函数实现对INI文件的读写操作。 要读取INI文件,我们可以使用C标准中的文件操作函数来打开并读取文件内容。首先,我们可以使用fopen函数打开INI文件,得到一个文件指针。然后,使用fgets函数逐行读取INI文件中的内容,根据INI文件的格式解析每一行的键值对。 要写入INI文件,我们同样可以使用C标准中的文件操作函数来创建或打开INI文件,并将键值对写入文件中。首先,使用fopen函数创建或打开一个INI文件,得到一个文件指针。然后,使用fprintf函数将键值对按照INI文件的格式写入文件中。 需要注意的是,在解析INI文件时,我们需要处理注释、空行和节(Section)的情况。注释和空行可以通过跳过以";"为起始的行来实现。而对于节,我们可以通过检查行是否以"["开头和以"]"结尾来确定。 总结起来,虽然C标准本身不提供直接读写INI文件的函数,但我们可以利用C标准中的文件操作函数来实现对INI文件的读写操作。这样可以有效地读取和修改INI文件中的键值对,实现对配置文件的管理。 ### 回答3: C标准中没有直接用于读写INI文件的函数。要读写INI文件,可以使用一些第三方或自己实现一个读写INI文件的函数。 一种常用的方案是使用Windows API中的GetPrivateProfileString和WritePrivateProfileString函数来读写INI文件。这两个函数提供了读写INI文件的功能。GetPrivateProfileString可以用于获取INI文件中的值,而WritePrivateProfileString可以用于修改或添加INI文件中的键值对。 另一种方案是使用一些第三方,例如libini,它提供了一组函数用于读写INI文件。这些函数可以完整地读取和修改INI文件,同时还提供了一些其他功能,例如验证INI文件的格式、删除INI文件中的一个节等。 如果需要实现自己的INI文件读写函数,可以使用C语言的文件读写功能来实现。可以使用fopen函数打开INI文件,使用fgets函数逐行读取INI文件的内容,使用sscanf函数解析每一行中的键值对。对于写入INI文件,可以使用fprintf函数将键值对写入文件中。 总而言之,C标准本身并不提供直接的INI文件读写功能,但可以通过使用Windows API中的函数、第三方或自己实现一个读写INI文件的函数来实现这个功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值