Click here to Skip to main content
13,090,541 members (61,285 online)
Click here to Skip to main content
Add your own
alternative version

Stats

214K views
14 downloads
73 bookmarked
Posted 27 Jun 2005

A Small Class to Read INI File

, 27 Jun 2005
Rate this:
Please Sign up or sign in to vote.
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

You may also be interested in...

Pro

Comments and Discussions

 
GeneralMy vote of 2 Pin
raiserle10-Apr-16 1:48
memberraiserle10-Apr-16 1:48 
QuestionThanks, its really save my time Pin
vjletter7-Sep-15 13:27
membervjletter7-Sep-15 13:27 
QuestionGood job, +5! Pin
_duDE_2-Jun-15 22:34
professional_duDE_2-Jun-15 22:34 
Questionfixed memory leak problem Pin
smart_dummies25-Mar-14 22:13
membersmart_dummies25-Mar-14 22:13 
AnswerRe: fixed memory leak problem Pin
Member 1095594129-Jul-14 9:02
memberMember 1095594129-Jul-14 9:02 
GeneralRe: fixed memory leak problem Pin
stevehoang2-Dec-14 7:53
memberstevehoang2-Dec-14 7:53 
AnswerRe: fixed memory leak problem Pin
itechdev5-Jul-15 13:49
memberitechdev5-Jul-15 13:49 
GeneralRe: fixed memory leak problem Pin
Jaime Stuardo - Chile18-Dec-16 6:20
memberJaime Stuardo - Chile18-Dec-16 6:20 
QuestionGreat piece of code Pin
John Connor28-Feb-14 17:09
memberJohn Connor28-Feb-14 17:09 
GeneralMy vote of 5 Pin
Member 995379625-Apr-13 2:36
memberMember 995379625-Apr-13 2:36 
GeneralShort and sweet. Pin
Functino29-Nov-12 14:31
memberFunctino29-Nov-12 14:31 
Questionyes very good but how do I download? lol Pin
fran kener15-Sep-12 3:41
memberfran kener15-Sep-12 3:41 
GeneralMy vote of 5 Pin
satthutinhoc30-Jul-11 22:50
membersatthutinhoc30-Jul-11 22:50 
GeneralMy vote of 5 Pin
mikkojay15-Sep-10 10:40
membermikkojay15-Sep-10 10:40 
QuestionKeep quotes? Pin
Member 362156629-Apr-10 17:53
memberMember 362156629-Apr-10 17:53 
GeneralAwesome! Pin
samaursa28-Nov-09 13:47
membersamaursa28-Nov-09 13:47 
GeneralRe: Awesome! Pin
samaursa28-Nov-09 13:49
membersamaursa28-Nov-09 13:49 
Questionwhy? Pin
Member 43037994-Nov-09 10:26
memberMember 43037994-Nov-09 10:26 
GeneralMy vote of 1 Pin
Jol1-Nov-09 16:20
memberJol1-Nov-09 16:20 
GeneralCannot Convert from Char* to LPCWSTR Pin
r_programmer11-Aug-09 1:28
memberr_programmer11-Aug-09 1:28 
GeneralRe: Cannot Convert from Char* to LPCWSTR Pin
Blaz10-Sep-09 3:40
memberBlaz10-Sep-09 3:40 
GeneralRe: Cannot Convert from Char* to LPCWSTR Pin
BIGLY5-Oct-09 8:43
memberBIGLY5-Oct-09 8:43 
GeneralRe: Cannot Convert from Char* to LPCWSTR Pin
Kendrick Hang26-Aug-10 13:42
memberKendrick Hang26-Aug-10 13:42 
GeneralReading all the sections in a INI file Pin
Al_S22-Jul-09 16:01
memberAl_S22-Jul-09 16:01 
GeneralThanks! [modified] Pin
burger8720-Apr-09 15:16
memberburger8720-Apr-09 15:16 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 27 Jun 2005
Article Copyright 2005 by xiaohe521
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid