Click here to Skip to main content
14,639,264 members
Articles » Languages » C / C++ Language » General
Article
Posted 27 Jun 2005

Stats

255.2K views
29 downloads
74 bookmarked

A Small Class to Read INI File

Rate this:
4.39 (40 votes)
Please Sign up or sign in to vote.
4.39 (40 votes)
27 Jun 2005CPOL
A small class to read INI File, only has 4 methods: ReadInteger, ReadFloat, ReadBoolean, ReadString.

Introduction

I have written two classes CIniReader and CIniWriter. They are used to read and write .Ini files. They only have four methods, and are very simple and useful.

IniReader.h

#ifndef INIREADER_H
#define INIREADER_H
class CIniReader
{
public:
 CIniReader(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_H

IniReader.cpp

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

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

IniWriter.h

#ifndef INIWRITER_H
#define INIWRITER_H
class CIniWriter
{
public:
 CIniWriter(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_H

IniWriter.cpp

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

Main.cpp

#include "iostream"
#include "IniWriter.h"
#include "IniReader.h"
int main(int argc, char * argv[])
{
 CIniWriter iniWriter(".\\Logger.ini");
 iniWriter.WriteString("Setting", "Name", "jianxx");   
 iniWriter.WriteInteger("Setting", "Age", 27); 
 iniWriter.WriteFloat("Setting", "Height", 1.82f); 
 iniWriter.WriteBoolean("Setting", "Marriage", false);  
 CIniReader iniReader(".\\Logger.ini");
 char *szName = iniReader.ReadString("Setting", "Name", "");   
 int iAge = iniReader.ReadInteger("Setting", "Age", 25); 
 float fltHieght = iniReader.ReadFloat("Setting", "Height", 1.80f); 
 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;   
}

History

  • 27th June, 2005: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

xiaohe521
Web Developer
China China
My Name is Xiangxiong Jian ,I love Programming ,And I love the sharing code

Comments and Discussions

 
GeneralThanks! [modified] Pin
burger8720-Apr-09 15:16
Memberburger8720-Apr-09 15:16 
QuestionRe: Thanks! [Please read previous post] Pin
burger8721-Apr-09 17:10
Memberburger8721-Apr-09 17:10 
Generalhel compile this code in visual c++ or in dev c++ Pin
domyprogam30-Mar-09 0:34
Memberdomyprogam30-Mar-09 0:34 
GeneralVery Helpful Pin
Nauman Azeem18-Jul-08 2:04
MemberNauman Azeem18-Jul-08 2:04 
GeneralThanks and congratulations Xiangxiong. Very simple, elegant code. Pin
kgaueee10-Oct-07 4:37
Memberkgaueee10-Oct-07 4:37 
GeneralEasy to use Pin
Paolo Bozzoli3-May-07 23:22
professionalPaolo Bozzoli3-May-07 23:22 
QuestionIs it possible to delete anything? Pin
hanne12319-Jan-07 4:15
Memberhanne12319-Jan-07 4:15 
AnswerRe: Is it possible to delete anything? Pin
Nick Alexeev16-Aug-07 17:32
professionalNick Alexeev16-Aug-07 17:32 
GeneralWorks Great! Thank You! Pin
Master_God15-Jan-07 1:45
MemberMaster_God15-Jan-07 1:45 
GeneralTrouble Compiling Pin
jrpohl1-Sep-06 7:33
Memberjrpohl1-Sep-06 7:33 
GeneralRe: Trouble Compiling Pin
marshman15-Oct-06 20:35
Membermarshman15-Oct-06 20:35 
GeneralCorrect Path Pin
Waldermort4-Aug-06 2:27
MemberWaldermort4-Aug-06 2:27 
GeneralGood one dude. Pin
jayender10-Apr-06 23:23
Memberjayender10-Apr-06 23:23 
GeneralThanks Pin
TehSausage20-Dec-05 11:34
MemberTehSausage20-Dec-05 11:34 
QuestionWhy no link to download? Pin
;hfdi[daokjgfpino[3-Nov-05 5:06
Member;hfdi[daokjgfpino[3-Nov-05 5:06 
QuestionHow to comment the INI? Pin
FlyingDancer21-Aug-05 18:13
MemberFlyingDancer21-Aug-05 18:13 
AnswerRe: How to comment the INI? Pin
Nick Alexeev19-Nov-06 20:50
professionalNick Alexeev19-Nov-06 20:50 
GeneralReadBoolean Pin
Roger6527-Jun-05 6:44
MemberRoger6527-Jun-05 6:44 
GeneralRe: ReadBoolean Pin
xiaohe52127-Jun-05 18:56
Memberxiaohe52127-Jun-05 18:56 
Generaln1 code Pin
pyrokar27-Jun-05 6:19
Memberpyrokar27-Jun-05 6:19 
GeneralRe: n1 code Pin
xiaohe52128-Jun-05 0:06
Memberxiaohe52128-Jun-05 0:06 
GeneralRe: n1 code Pin
pyrokar28-Jun-05 3:15
Memberpyrokar28-Jun-05 3:15 
GeneralNice Pin
ThatsAlok27-Jun-05 1:48
MemberThatsAlok27-Jun-05 1:48 
Generalthanks for your sharing your code Pin
wanttoflyfish27-Jun-05 1:25
Memberwanttoflyfish27-Jun-05 1:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.