C++ reading INI configuration file class example

  • 2020-04-02 02:27:22
  • OfStack

This article illustrates the C++ method of reading configuration files with examples.

In general, we all like to use the ini file extension as a configuration file that can be read and modify the variable values, also can set the new group, a new variable, in this paper, the definition of an instance of the code is read ini file, the other is a CIniFile class implementation file, a combination of these, vc + + perfect realization of the ini file to read and write.

In the member functions SetVarStr and SetVarInt, when iType is equal to zero, a new variable is written if the user-specified parameter does not exist in the ini file. When iType is not equal to zero, if the user-specified parameter does not exist in the ini file, the new variable is not written, but FALSE is directly returned. The program code is shown below, and some key points are explained by comments:



#ifndef _CINIFILE_H_
#define _CINIFILE_H_
#include <afxtempl.h>
//User interface description: in the member functions SetVarStr and SetVarInt, when iType is equal to zero, if the parameters specified by the user do not exist in the ini file,
//When iType is not equal to zero, if the user-specified parameter does not exist in the ini file, the new variable is not written, and FALSE is directly returned.
class CIniFile
{
public:
 CIniFile();
 virtual ~CIniFile();
private:
 CIniFile(const CIniFile &);
 CIniFile & operator = (const CIniFile &);
public:
 //Create a function
 BOOL Create(const CString &strFileName);
 //Get the integer value of the variable
 BOOL GetVarInt(const CString &,const CString & ,int &);
 //Get the string value of the variable
 BOOL GetVarStr(const CString &,const CString & ,CString & );
 //Resets the integer value of the variable
 BOOL SetVarInt(const CString &,const CString & ,const int &,const int iType = 1);
 //Resets the string value of the variable
 BOOL SetVarStr(const CString &,const CString &, const CString &,const int iType = 1);
private:
 BOOL GetVar(const CString &,const CString &,CString &);
 BOOL SetVar(const CString &,const CString &,const CString &,const int iType = 1);
 int SearchLine(const CString &,const CString &);
private:
 CArray FileContainer;
 BOOL bFileExsit;
 CStdioFile stfFile;
 CString strInIFileName;
};
#endif
CIniFile.cpp File contents: 
 
#include <afxtempl.h>
#include "CIniFile.h"
CIniFile::CIniFile():bFileExsit(FALSE)
{
}
CIniFile::~CIniFile()
{
if(bFileExsit)
{
if(stfFile.Open(strInIFileName,CFile::modeCreate | CFile::modeWrite) && FileContainer.GetSize() > 0)
{
CString strParam;
for(int i = 0; i< FileContainer.GetSize();i++)
{
strParam = FileContainer[i];
// stfFile.WriteString(strParam);
stfFile.WriteString(strParam+_T("n"));
}
}
stfFile.Close();
}
if(FileContainer.GetSize() > 0)
{
FileContainer.RemoveAll();
}
}
BOOL CIniFile::Create(const CString & strFileName)
{
bFileExsit = FALSE;
strInIFileName = strFileName;
if(!stfFile.Open(strFileName,CFile::modeRead))
{
return bFileExsit;
}
CString strFileLine;
while(bFileExsit = stfFile.ReadString(strFileLine))
{
if(bFileExsit == FALSE)
return bFileExsit;
FileContainer.Add(strFileLine);
}
stfFile.Close();
bFileExsit = TRUE;
return bFileExsit;
}
BOOL CIniFile::GetVar(const CString & strSection,const CString & strVarName,CString &strReturnValue)
{
if(bFileExsit == FALSE || FileContainer.GetSize() < 0)
return bFileExsit;
int iLine = SearchLine(strSection,strVarName);
if(iLine > 0)
{
CString strParam = FileContainer[iLine -1];
strReturnValue = strParam.Mid(strParam.Find(_T("=")) + 1);
return TRUE;
}
return FALSE;
}
BOOL CIniFile::GetVarStr(const CString & strSection,const CString & strVarName,CString &strReturnValue)
{
return(GetVar(strSection,strVarName,strReturnValue));
}
BOOL CIniFile::GetVarInt(const CString & strSection,const CString & strVarName,int & iValue)
{
CString strReturnVar;
if(GetVar(strSection,strVarName,strReturnVar))
{
strReturnVar.TrimLeft();
int iLen = strReturnVar.GetLength();
iValue = _tstoi(strReturnVar.GetBuffer(iLen));
return TRUE;
}
return TRUE;
}
BOOL CIniFile::SetVar(const CString & strSection,const CString & strVarName,const CString & strVar,const int iType)
{
if(bFileExsit == FALSE )
return bFileExsit;
if(FileContainer.GetSize() == 0)
{
FileContainer.Add(_T("[") + strSection + _T("]"));
FileContainer.Add(strVarName + _T("=") + strVar);
return TRUE;
}
int i = 0;
int iFileLines = (int)FileContainer.GetSize();
//for(pInterator;pInterator != FileContainer.end();++pInterator)
//{
while(i< iFileLines)
{
CString strValue = FileContainer.GetAt(i++);
strValue.TrimLeft();
if((strValue.Find(_T("[")) >=0) && (strValue.Find(strSection) >=0))
{ 
while(i < iFileLines)
{
CString strSectionList = FileContainer[i++];
strSectionList.TrimLeft();
if(strSectionList.Find(_T("//")) >=0)//find Comment lines 
continue;
if(strSectionList.Find(strVarName)>=0)//find
{
CString strParam = strVarName + "=" + strVar;
//FileContainer.SetAt(i-1,strParam);
FileContainer[i-1] = strParam;
return TRUE;
}
if(strSectionList.Find(_T("["),0)>=0)//In the SECTION of the original file, there's no corresponding variable to add and, in this case, there's another SECTION underneath
{
//The process works like this, first moving the current value backward, then adding the new value at the current position
if(iType !=0)
return FALSE;
CString strParam;
FileContainer.Add(strParam);
int iPre = (int)(FileContainer.GetSize()-1);
while(iPre >= i)
{
CString strBehind = FileContainer[iPre -1];
FileContainer[iPre] = strBehind;
iPre --;
}
strParam = strVarName + _T("=") + strVar;
FileContainer.SetAt(i-1,strParam);
return TRUE;
}
if(i == iFileLines && iType == 0)
{
FileContainer.Add(strVarName + _T("=") + strVar);
return TRUE;
}
}
}
}
if(iType == 0)
{
FileContainer.Add(_T("[") + strSection + _T("]"));
FileContainer.Add(strVarName + _T("=") + strVar);
}
return TRUE;
}
BOOL CIniFile::SetVarStr(const CString & strSection,const CString & strVarName,const CString & strValue,const int iType)
{
return SetVar(strSection,strVarName,strValue,iType);
}
BOOL CIniFile::SetVarInt(const CString & strSection,const CString & strVarName,const int & iValue,const int iType)
{
CString strVar;
strVar.Format(_T("%d"),iValue);
return (SetVar(strSection,strVarName,strVar,iType));
}
int CIniFile::SearchLine(const CString & strSection,const CString & strVarName)
{
if(FileContainer.GetSize() > 0)
{
int i = 0;
int iFileLines = (int)FileContainer.GetSize();
while(i< iFileLines)
{
CString strValue = FileContainer[i++];
strValue.TrimLeft();
if(strValue.Find(_T("[")) >=0 && strValue.Find(strSection,1)>=0)
{ 
while(i < iFileLines)
{
CString strSectionList = FileContainer[i++];
strSectionList.TrimLeft();
if(strSectionList.Find(_T("//")) >=0)//find Comment lines 
continue;
if(strSectionList.Find(strVarName)>=0)//find
{
return i;
}
if(strSectionList.Find(_T("["),0) >= 0)//Another paragraph appears, looking for failures
{
return -2;
}
}
}
}
}
return -1;
}

Related articles: