千家信息网

ini文件的读取

发表于:2025-02-07 作者:千家信息网编辑
千家信息网最后更新 2025年02月07日,ini.h代码#ifndef INI_H#define INI_H#include #include using namespace std;#define CONFIGLEN 256enum INI
千家信息网最后更新 2025年02月07日ini文件的读取

ini.h代码

#ifndef INI_H

#define INI_H


#include

#include

using namespace std;


#define CONFIGLEN 256


enum INI_RES

{

INI_SUCCESS, //成功

INI_ERROR, //普通错误

INI_OPENFILE_ERROR, //打开文件失败

INI_NO_ATTR //无对应的键值

};


// 子键索引 子键值

typedef map KEYMAP;

// 主键索引 主键值

typedef map MAINKEYMAP;

// config 文件的基本操作类


class CIni

{

public:

// 构造函数

CIni();


// 析够函数

virtual ~CIni();

public:

//获取×××的键值

int GetInt(const char* mAttr, const char* cAttr);

//获取键值的字符串

char *GetStr(const char* mAttr, const char* cAttr);

// 打开config 文件

INI_RES OpenFile(const char* pathName, const char* type);

// 关闭config 文件

INI_RES CloseFile();

protected:

// 读取config文件

INI_RES GetKey(const char* mAttr, const char* cAttr, char* value);

protected:

// 被打开的文件局柄

FILE* m_fp;

char m_szKey[CONFIGLEN];

MAINKEYMAP m_Map;

};


#endif


ini.cpp代码

#include "ini.h"

/******************************************************************************

* 功 能:构造函数

* 参 数:无

* 返回值:无

* 备 注:

******************************************************************************/

CIni::CIni()

{

memset(m_szKey, 0, sizeof(m_szKey));

m_fp = NULL;

}


/******************************************************************************

* 功 能:析构函数

* 参 数:无

* 返回值:无

* 备 注:

******************************************************************************/


CIni::~CIni()

{

m_Map.clear();

}


/******************************************************************************

* 功 能:打开文件函数

* 参 数:无

* 返回值:

* 备 注:

******************************************************************************/

INI_RES CIni::OpenFile(const char* pathName, const char* type)

{

string szLine, szMainKey, szLastMainKey, szSubKey;

char strLine[CONFIGLEN] = { 0 };

KEYMAP mLastMap;

int nIndexPos = -1;

int nLeftPos = -1;

int nRightPos = -1;

m_fp = fopen(pathName, type);


if (m_fp == NULL)

{

printf("open inifile %s error!\n", pathName);

return INI_OPENFILE_ERROR;

}


m_Map.clear();


while (fgets(strLine, CONFIGLEN, m_fp))

{

szLine.assign(strLine);

//删除字符串中的非必要字符

nLeftPos = szLine.find("\n");

if (string::npos != nLeftPos)

{

szLine.erase(nLeftPos, 1);

}

nLeftPos = szLine.find("\r");

if (string::npos != nLeftPos)

{

szLine.erase(nLeftPos, 1);

}

//判断是否是主键

nLeftPos = szLine.find("[");

nRightPos = szLine.find("]");

if (nLeftPos != string::npos && nRightPos != string::npos)

{

szLine.erase(nLeftPos, 1);

nRightPos--;

szLine.erase(nRightPos, 1);

m_Map[szLastMainKey] = mLastMap;

mLastMap.clear();

szLastMainKey = szLine;

}

else

{

//是否是子键

if (nIndexPos = szLine.find("="), string::npos != nIndexPos)

{

string szSubKey, szSubValue;

szSubKey = szLine.substr(0, nIndexPos);

szSubValue = szLine.substr(nIndexPos + 1, szLine.length() - nIndexPos - 1);

mLastMap[szSubKey] = szSubValue;

}

else

{

//TODO:不符合ini键值模板的内容 如注释等

}

}


}

//插入最后一次主键

m_Map[szLastMainKey] = mLastMap;


return INI_SUCCESS;

}


/******************************************************************************

* 功 能:关闭文件函数

* 参 数:无

* 返回值:

* 备 注:

******************************************************************************/

INI_RES CIni::CloseFile()

{



if (m_fp != NULL)

{

fclose(m_fp);

m_fp = NULL;

}


return INI_SUCCESS;

}


/******************************************************************************

* 功 能:获取[SECTION]下的某一个键值的字符串

* 参 数:

* char* mAttr 输入参数 主键

* char* cAttr 输入参数 子键

* char* value 输出参数 子键键值

* 返回值:

* 备 注:

******************************************************************************/

INI_RES CIni::GetKey(const char* mAttr, const char* cAttr, char* pValue)

{


KEYMAP mKey = m_Map[mAttr];


string sTemp = mKey[cAttr];


strcpy(pValue, sTemp.c_str());


return INI_SUCCESS;

}


/******************************************************************************

* 功 能:获取×××的键值

* 参 数:

* cAttr 主键

* cAttr 子键

* 返回值:正常则返回对应的数值 未读取成功则返回0(键值本身为0不冲突)

* 备 注:

******************************************************************************/

int CIni::GetInt(const char* mAttr, const char* cAttr)

{

int nRes = 0;


memset(m_szKey, 0, sizeof(m_szKey));


if (INI_SUCCESS == GetKey(mAttr, cAttr, m_szKey))

{

nRes = atoi(m_szKey);

}

return nRes;

}


/******************************************************************************

* 功 能:获取键值的字符串

* 参 数:

* cAttr 主键

* cAttr 子键

* 返回值:正常则返回读取到的子键字符串 未读取成功则返回"NULL"

* 备 注:

******************************************************************************/

char *CIni::GetStr(const char* mAttr, const char* cAttr)

{

memset(m_szKey, 0, sizeof(m_szKey));


if (INI_SUCCESS != GetKey(mAttr, cAttr, m_szKey))

{

strcpy(m_szKey, "NULL");

}


return m_szKey;


}


config.ini


[cctv.thrift]

IP=192.168.37.123

Port=7001

username=admin

password=admin


测试


static void ReadIniFile()

{

CIni ini;

ini.OpenFile("config.ini", "r");

char *pVal1 = ini.GetStr("cctv.thrift", "IP");

int nKey = ini.GetInt("cctv.thrift", "Port");

}


注意

GetStr返回的字符串,必须马上保存到其他的变量中,如果这个时候重新调用GetInt,上面的pVal1的值将会改变


0