借鉴了别人的。https://blog.csdn.net/zhouyongku/article/details/44857821
头文件
#include <iostream> #include <map> #include <string> #include <memory.h> #include <stdio.h> #include <stdlib.h> using namespace std; #define CONFIGLEN 256 enum INI_RES { INI_SUCCESS, INI_ERROR, INI_OPENFILE_ERROR, INI_NO_ATTR }; typedef map<std::string, std::string> KEYMAP; typedef map<std::string, KEYMAP> MAINKEYMAP; class CIni { public: CIni(); virtual ~CIni(); public: int GetInt(const char* mAttr, const char* cAttr); INI_RES GetStr(const char* mAttr, const char* cAttr, char* szValue); INI_RES OpenFile(const char* pathName, const char* type); INI_RES CloseFile(); protected: INI_RES GetKey(const char* mAttr, const char* cAttr, char* value); FILE *m_fp; MAINKEYMAP m_Map; };
实现文件
#include "Ini.h" CIni::CIni() { 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 iIndexPos = -1; int iLeftPos = -1; int iRightPos = -1; m_fp = fopen(pathName, type); if(!m_fp) { printf("open ini file %s error!\n", pathName); return INI_OPENFILE_ERROR; } m_Map.clear(); while(fgets(strLine, CONFIGLEN, m_fp)) { //cout << szLine << endl; szLine.assign(strLine); iLeftPos = szLine.find("\n"); if(string::npos != iLeftPos) { szLine.erase(iLeftPos, 1); } iLeftPos = szLine.find("\r"); if(string::npos != iLeftPos) { szLine.erase(iLeftPos, 1); } iLeftPos = szLine.find("["); iRightPos = szLine.find("]"); if(iLeftPos != string::npos && iRightPos != string::npos) { szLine.erase(iLeftPos, 1); iRightPos--; szLine.erase(iRightPos, 1); m_Map[szLastMainKey] = mLastMap; mLastMap.clear(); szLastMainKey = szLine; } else { if(iIndexPos = szLine.find("="), string::npos != iIndexPos) { string szSubKey, szSubValue; szSubKey = szLine.substr(0, iIndexPos); szSubValue = szLine.substr(iIndexPos + 1, szLine.length() - iIndexPos - 1); //cout << "szSubValue:" << szSubValue << endl; mLastMap[szSubKey] = szSubValue; } else { } } } m_Map[szLastMainKey] = mLastMap; return INI_SUCCESS; } INI_RES CIni::CloseFile() { if(!m_fp) { fclose(m_fp); m_fp = NULL; } return INI_SUCCESS; } 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()); //cout << "pValue:" << pValue << endl; return INI_SUCCESS; } int CIni::GetInt(const char* mAttr, const char* cAttr) { char szKey[CONFIGLEN]; memset(szKey, 0, sizeof(szKey)); if(INI_SUCCESS == GetKey(mAttr, cAttr, szKey)) { return atoi(szKey); } return 0; } INI_RES CIni::GetStr(const char* mAttr, const char* cAttr, char* szValue) { if(INI_SUCCESS != GetKey(mAttr, cAttr, szValue)) { strcpy(szValue, "NULL"); return INI_ERROR; } //cout << "szValue:" << szValue << endl; return INI_SUCCESS; }
测试
#include <iostream> #include "Ini.h" using namespace std; int main() { CIni ini; ini.OpenFile("./test.ini", "r"); char szVal1[CONFIGLEN]; char szVal3[CONFIGLEN]; memset(szVal1, 0, sizeof(szVal1)); memset(szVal3, 0, sizeof(szVal3)); int iVal2, iVal4; ini.GetStr("root", "ip", szVal1); iVal2 = ini.GetInt("root", "port"); ini.GetStr("ser", "ip", szVal3); iVal4 = ini.GetInt("ser", "port"); cout << "pVal1:" << szVal1 << endl; cout << "iVal2:" << iVal2 << endl; cout << "pVal3:" << szVal3 << endl; cout << "iVal4:" << iVal4 << endl; return 0; }
pVal1:192.168.1.1 iVal2:111 pVal3:192.168.1.2 iVal4:222
为保持和GetStr格式一致,可添加如下接口
INI_RES GetInt(const char* mAttr, const char* cAttr, int &iValue);
接口实现
INI_RES CIni::GetInt(const char* mAttr, const char* cAttr, int &iValue) { char szKey[CONFIGLEN]; memset(szKey, 0, sizeof(szKey)); if(INI_SUCCESS == GetKey(mAttr, cAttr, szKey)) { iValue = atoi(szKey); return INI_SUCCESS; } return INI_ERROR; }
调用
ini.GetInt("root", "port", iVal2);
