【C++】ini 配置文件读写

main.cpp

#include "iostream"
#include "IniWriter.h"
#include "IniReader.h"
int main(int argc, char * argv[])
{
    IniWriter iniWriter(".\\Logger.ini");
    iniWriter.WriteString("Setting", "Name", "mhyr");
    iniWriter.WriteInteger("Setting", "Age", 22);
    iniWriter.WriteFloat("Setting", "Height", 1.82f);
    iniWriter.WriteBoolean("Setting", "Marriage", false);

    IniReader iniReader(".\\Logger.ini");
    char *szName = iniReader.ReadString("Setting", "Name", "");
    int iAge = iniReader.ReadInteger("Setting", "Age", 0);
    float fltHieght = iniReader.ReadFloat("Setting", "Height", 0.0f);
    bool bMarriage = iniReader.ReadBoolean("Setting", "Marriage", true);

    std::cout<<"Name:"<<szName<<std::endl
             <<"Age:"<<iAge<<std::endl
             <<"Height:"<<fltHieght<<std::endl
             <<"Marriage:"<<bMarriage<<std::endl;
    delete szName;
    return 1;
}

IniReader.h

#ifndef INIFILE_INIREADER_H
#define INIFILE_INIREADER_H

class IniReader
{
public:
    IniReader(char* szFileName);
    int ReadInteger(char* szSection, char* szKey, int iDefaultValue);
    float ReadFloat(char* szSection, char* szKey, float fltDefaultValue);
    bool ReadBoolean(char* szSection, char* szKey, bool bolDefaultValue);
    char* ReadString(char* szSection, char* szKey, const char* szDefaultValue);
private:
    char m_szFileName[255];
};

#endif

IniReader.cpp

#include "IniReader.h"
#include <iostream>
#include <Windows.h>

IniReader::IniReader(char* szFileName)
{
    memset(m_szFileName, 0x00, 255);
    memcpy(m_szFileName, szFileName, strlen(szFileName));
}
int IniReader::ReadInteger(char* szSection, char* szKey, int iDefaultValue)
{
    int iResult = GetPrivateProfileIntA(szSection,  szKey, iDefaultValue, m_szFileName);
    return iResult;
}
float IniReader::ReadFloat(char* szSection, char* szKey, float fltDefaultValue)
{
    char szResult[255];
    char szDefault[255];
    float fltResult;
    sprintf(szDefault, "%f",fltDefaultValue);
    GetPrivateProfileStringA(szSection,  szKey, szDefault, szResult, 255, m_szFileName);
    fltResult =  atof(szResult);
    return fltResult;
}
bool IniReader::ReadBoolean(char* szSection, char* szKey, bool bolDefaultValue)
{
    char szResult[255];
    char szDefault[255];
    bool bolResult;
    sprintf(szDefault, "%s", bolDefaultValue? "True" : "False");
    GetPrivateProfileStringA(szSection, szKey, szDefault, szResult, 255, m_szFileName);
    bolResult =  (strcmp(szResult, "True") == 0 ||
                  strcmp(szResult, "true") == 0) ? true : false;
    return bolResult;
}
char* IniReader::ReadString(char* szSection, char* szKey, const char* szDefaultValue)
{
    char* szResult = new char[255];
    memset(szResult, 0x00, 255);
    GetPrivateProfileStringA(szSection,  szKey,
                            szDefaultValue, szResult, 255, m_szFileName);
    return szResult;
}

IniWriter.h

#ifndef INIFILE_INIWRITER_H
#define INIFILE_INIWRITER_H


class IniWriter
{
public:
    IniWriter(char* szFileName);
    void WriteInteger(char* szSection, char* szKey, int iValue);
    void WriteFloat(char* szSection, char* szKey, float fltValue);
    void WriteBoolean(char* szSection, char* szKey, bool bolValue);
    void WriteString(char* szSection, char* szKey, char* szValue);
private:
    char m_szFileName[255];
};


#endif

IniWriter.cpp

#include "IniWriter.h"
#include <iostream>
#include <Windows.h>
IniWriter::IniWriter(char* szFileName)
{
    memset(m_szFileName, 0x00, 255);
    memcpy(m_szFileName, szFileName, strlen(szFileName));
}
void IniWriter::WriteInteger(char* szSection, char* szKey, int iValue)
{
    char szValue[255];
    sprintf(szValue, "%d", iValue);
    //WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName);
    WritePrivateProfileStringA(szSection,  szKey, szValue, m_szFileName);
}
void IniWriter::WriteFloat(char* szSection, char* szKey, float fltValue)
{
    char szValue[255];
    sprintf(szValue, "%f", fltValue);
    WritePrivateProfileStringA(szSection,  szKey, szValue, m_szFileName);
}
void IniWriter::WriteBoolean(char* szSection, char* szKey, bool bolValue)
{
    char szValue[255];
    sprintf(szValue, "%s", bolValue ? "True" : "False");
    WritePrivateProfileStringA(szSection,  szKey, szValue, m_szFileName);
}
void IniWriter::WriteString(char* szSection, char* szKey, char* szValue)
{
    WritePrivateProfileStringA(szSection,  szKey, szValue, m_szFileName);
}

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
c ini配置文件读写是一种常见的配置文件读写方式。在C语言中,通常使用文件操作函数来读写ini配置文件读取ini文件首先需要打开文件,可以使用fopen函数打开文件,并指定打开方式为"r"(只读方式)。然后逐行读取文件内容,可以使用fgets函数逐行读取读取到的每一行字符串都可以通过字符串处理函数进行进一步操作,例如使用strtok函数将行字符串分割成键值对。 对于每一行的键值对,可以进一步使用字符串处理函数进行解析。可以使用strchr函数找到等号(=)的位置,将键和值分隔开。然后可以使用strcpy或strncpy函数将键和值分别复制到变量中,并进行相应的后续处理。 写入ini文件也需要打开文件,可以使用fopen函数打开文件,并指定打开方式为"w"(写入方式)。然后可以使用fprintf函数将配置项写入文件。具体的操作是先写入键的字符串,然后写入等号(=),最后写入值的字符串。写入完毕后,可以使用fclose函数关闭文件。 需要注意的是,在读取和写入ini文件时,需要进行错误处理,例如检查文件是否打开成功、是否成功读写、文件关闭时是否出错等。这样可以保证程序的健壮性。 总之,对于C语言来说,ini配置文件读写是一种比较简单和常见的操作,通过使用文件操作函数和字符串处理函数,可以方便地读取和写入ini文件中的配置项。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值