C++ 读写INI配置文件代码

/*****************************************************************************
 * @File Name: INIReader.h
 * @Author: HongQiang
 * @EMail: 2584456944@qq.com
 * @Date: 2021-05-10 10:25:14
 * @LastEditTime: 2021-06-07 20:07:15
 *******************************************************************************/
#ifndef INIREADER_H
#define INIREADER_H

#include <iostream>

class INIReader 
{
public:
    explicit INIReader() {}
    ~INIReader() {}

    /*****************************************************************************
     * @descripttion:GetIniKeyString 
     * @param: title    配置文件中一组数据的标识 
               key      这组数据中要读出的值的标识 
               filename 要读取的文件路径
     * @return: 找到需要查的值则返回正确结果 
                否则返回NULL 
     *******************************************************************************/
    char *GetIniKeyString(const std::string& title,const std::string& key, const std::string& filename);

    /*****************************************************************************
     * @descripttion:GetIniKeyInt 
     * @param: title    配置文件中一组数据的标识 
               key      这组数据中要读出的值的标识 
               filename 要读取的文件路径
     * @return: 找到需要查的值则返回正确结果 
                否则返回NULL 
     *******************************************************************************/
    int GetIniKeyInt(const std::string& title,const std::string& key,const std::string& filename);

    /*****************************************************************************
     * @descripttion:PutIniKeyString 
     * @param: title    配置文件中一组数据的标识 
               key      这组数据中要读出的值的标识 
               value    更改后的值
               filename 要读取的文件路径
     * @return: 成功返回  0 
                否则返回 -1 
     *******************************************************************************/
    int PutIniKeyString(const std::string& title,const std::string& key,const std::string& val,const std::string& filename);

    /*****************************************************************************
     * @descripttion:PutIniKeyInt
     * @param: title    配置文件中一组数据的标识 
               key      这组数据中要读出的值的标识 
               value    更改后的值
               filename 要读取的文件路径
     * @return: 成功返回  0 
                否则返回 -1 
     *******************************************************************************/
    int PutIniKeyInt(const std::string& title,const std::string& key,const int& val,const std::string& filename);
};

#endif
/*****************************************************************************
 * @File Name: INIReader.cpp
 * @Author: HongQiang
 * @EMail: 2584456944@qq.com
 * @Date: 2021-05-10 13:51:00
 * @LastEditTime: 2021-05-10 14:07:27
 *******************************************************************************/
#include "INIReader.h"

char * INIReader::GetIniKeyString(const std::string& title,const std::string& key, const std::string& filename)
{
    FILE *fp;
    int  flag = 0;
    char sTitle[32];
    char *wTmp;
    static char sLine[1024];

    sprintf(sTitle, "[%s]", title.c_str());
    if(NULL == (fp = fopen(filename.c_str(), "r")))
    {
        perror("fopen");
        return NULL;
    }  

    while (NULL != fgets(sLine, 1024, fp))
    {  

        if (0 == strncmp("//", sLine, 2))
            continue;
        if ('#' == sLine[0])
            continue;
        wTmp = strchr(sLine, '=');
        if ((NULL != wTmp) && (1 == flag))
        {
            if (0 == strncmp(key.c_str(), sLine, wTmp-sLine))
            { // 长度依文件读取的为准  
                sLine[strlen(sLine) - 1] = '\0';
                fclose(fp);
                return wTmp + 1;  
            }
        }
        else
        {
            if (0 == strncmp(sTitle, sLine, strlen(sLine) - 1))
            {
                // 长度依文件读取的为准
                flag = 1; // 找到标题位置
            }
        }
    }
    fclose(fp);
    return NULL;
}

int INIReader::GetIniKeyInt(const std::string& title,const std::string& key,const std::string& filename)
{
    char * temp = GetIniKeyString(title, key, filename);
    if(NULL == temp)
    {
        LOG(LOG_ERR, "GetIniKeyInt Error!");
        return -1;
    }
    return atoi(temp);
}

int INIReader::PutIniKeyString(const std::string& title,const std::string& key,const std::string& val,const std::string& filename)
{
    FILE *fpr, *fpw;
    int  flag = 0;
    char sLine[1024], sTitle[32],*wTmp;

    sprintf(sTitle, "[%s]", title.c_str());
    if (NULL == (fpr = fopen(filename.c_str(), "r")))
        printf("open read file faied!");
    sprintf(sLine, "%s.tmp", filename.c_str());
    if (NULL == (fpw = fopen(sLine, "w")))
        printf("open write file faied!");

    while (NULL != fgets(sLine, 1024, fpr)) 
    {
        if (2 != flag) 
        { // 如果找到要修改的那一行,则不会执行内部的操作  
            wTmp = strchr(sLine, '=');
            if ((NULL != wTmp) && (1 == flag))
            {
                if (0 == strncmp(key.c_str(), sLine, wTmp-sLine))
                { // 长度依文件读取的为准  
                    flag = 2;// 更改值,方便写入文件
                    sprintf(wTmp + 1, "%s\n", val.c_str());
                }
            } else {
                if (0 == strncmp(sTitle, sLine, strlen(sLine) - 1))
                { // 长度依文件读取的为准
                    flag = 1; // 找到标题位置
                }
            }
        }
        fputs(sLine, fpw); // 写入临时文件
    }
    fclose(fpr);
    fclose(fpw);
    sprintf(sLine, "%s.tmp", filename.c_str());
    return rename(sLine, filename.c_str());// 将临时文件更新到原文
}

int INIReader::PutIniKeyInt(const std::string& title,const std::string& key,const int& val,const std::string& filename)
{
    char sVal[32];
    sprintf(sVal, "%d", val);
    return PutIniKeyString(title, key, sVal, filename.c_str());
}
点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注