Click here to Skip to main content
6,594,432 members and growing! (13,719 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate License: The Code Project Open License (CPOL)

A Small Class to Read INI File

By xiaohe521

A small class to read INI File, only has 4 methods: ReadInteger, ReadFloat, ReadBoolean, ReadString.
C++ (VC6), Windows, Visual Studio, Dev
Version:3 (See All)
Posted:27 Jun 2005
Views:62,411
Bookmarked:34 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
24 votes for this article.
Popularity: 5.17 Rating: 3.75 out of 5
3 votes, 12.5%
1
2 votes, 8.3%
2

3
7 votes, 29.2%
4
12 votes, 50.0%
5

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)

About the Author

xiaohe521


Member
My Name is Xiangxiong Jian ,I love Programming ,And I love the sharing code
Occupation: Web Developer
Location: China China

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 30 (Total in Forum: 30) (Refresh)FirstPrevNext
Questionwhy? PinmemberMember 430379911:26 4 Nov '09  
GeneralMy vote of 1 PinmemberJol17:20 1 Nov '09  
GeneralCannot Convert from Char* to LPCWSTR Pinmemberr_programmer2:28 11 Aug '09  
GeneralRe: Cannot Convert from Char* to LPCWSTR PinmemberBlaz4:40 10 Sep '09  
GeneralRe: Cannot Convert from Char* to LPCWSTR PinmemberBIGLY9:43 5 Oct '09  
GeneralReading all the sections in a INI file PinmemberAl_S17:01 22 Jul '09  
GeneralThanks! [modified] Pinmemberburger8716:16 20 Apr '09  
QuestionRe: Thanks! [Please read previous post] Pinmemberburger8718:10 21 Apr '09  
Generalhel compile this code in visual c++ or in dev c++ Pinmemberdomyprogam1:34 30 Mar '09  
GeneralVery Helpful PinmemberMember 14688463:04 18 Jul '08  
GeneralThanks and congratulations Xiangxiong. Very simple, elegant code. Pinmemberkgaueee5:37 10 Oct '07  
GeneralEasy to use PinmemberSbarBaz0:22 4 May '07  
QuestionIs it possible to delete anything? Pinmemberhanne1235:15 19 Jan '07  
AnswerRe: Is it possible to delete anything? PinmemberNick Alexeev18:32 16 Aug '07  
GeneralWorks Great! Thank You! PinmemberMaster_God2:45 15 Jan '07  
GeneralTrouble Compiling Pinmemberjrpohl8:33 1 Sep '06  
GeneralRe: Trouble Compiling Pinmembermarshman21:35 15 Oct '06  
GeneralCorrect Path Pinmemberwaldermort3:27 4 Aug '06  
GeneralGood one dude. Pinmemberjayender0:23 11 Apr '06  
GeneralThanks PinmemberTehSausage12:34 20 Dec '05  
GeneralWhy no link to download? Pinmember;hfdi[daokjgfpino[6:06 3 Nov '05  
GeneralHow to comment the INI? PinmemberFlyingDancer19:13 21 Aug '05  
GeneralRe: How to comment the INI? PinmemberNick Alexeev21:50 19 Nov '06  
GeneralReadBoolean PinmemberRoger657:44 27 Jun '05  
GeneralRe: ReadBoolean Pinmemberxiaohe52119:56 27 Jun '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Jun 2005
Editor: Deeksha Shenoy
Copyright 2005 by xiaohe521
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project