Click here to Skip to main content
15,888,454 members
Articles / Programming Languages / Visual C++ 10.0
Tip/Trick

A Small Class to Read INI Files

Rate me:
Please Sign up or sign in to vote.
2.67/5 (3 votes)
30 Apr 2012CPOL 14.1K   9   2
This is a TCHAR alternative for "A Small Class to Read INI File"

Introduction 

I have adapted xiaohe521's CIniReader and CIniWriter classes (LINK) to work with TCHAR instead of char. This way, you can use it either in one- or two-byte character sets. 

This is my first post, so I'm always glad to get your feedback! ;) 

Background  

I wanted to use the aforementioned classes in Visual Studio 2010 and needed to adapt them in order to make the code compile.   

Using the code   

As I didn't change the way the code is working I ask you to check out xiaohe521's description here! Of course your main-program hast to work with TCHARs, too!   

This is what the new code looks like:  

IniReader.h

C++
//// Source: http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File ////

#ifndef INIREADER_H
#define INIREADER_H

#include <windows.h>

class CIniReader
{
public:
 CIniReader(LPCTSTR szFileName); 
 int ReadInteger(LPCTSTR szSection, LPCTSTR szKey, int iDefaultValue);
 float ReadFloat(LPCTSTR szSection, LPCTSTR szKey, float fltDefaultValue);
 bool ReadBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolDefaultValue);
 LPTSTR ReadString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szDefaultValue);
private:
  TCHAR m_szFileName[255];
};
#endif //INIREADER_H

IniReader.cpp 

C++
//// Source: http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File ////

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

CIniReader::CIniReader(LPCTSTR szFileName)
{
 memset(m_szFileName, 0x00, sizeof(m_szFileName));
 memcpy(m_szFileName, szFileName, _tcslen(szFileName)*sizeof(TCHAR));
}
int CIniReader::ReadInteger(LPCTSTR szSection, LPCTSTR szKey, int iDefaultValue)
{
 int iResult = GetPrivateProfileInt(szSection,  szKey, iDefaultValue, m_szFileName); 
 return iResult;
}
float CIniReader::ReadFloat(LPCTSTR szSection, LPCTSTR szKey, float fltDefaultValue)
{
 TCHAR szResult[255];
 TCHAR szDefault[255];
 float fltResult;
 _stprintf_s(szDefault, 255, TEXT("%f"),fltDefaultValue);
 GetPrivateProfileString(szSection,  szKey, szDefault, szResult, 255, m_szFileName); 
 fltResult =  (float)_tstof(szResult);
 return fltResult;
}
bool CIniReader::ReadBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolDefaultValue)
{
 TCHAR szResult[255];
 TCHAR szDefault[255];
 bool bolResult;
 _stprintf_s(szDefault, 255, TEXT("%s"), bolDefaultValue? TEXT("True") : TEXT("False"));
 GetPrivateProfileString(szSection, szKey, szDefault, szResult, 255, m_szFileName); 
 bolResult =  (_tcscmp(szResult, TEXT("True")) == 0 || 
		_tcscmp(szResult, TEXT("true")) == 0) ? true : false;
 return bolResult;
}
LPTSTR CIniReader::ReadString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szDefaultValue)
{
 LPTSTR szResult = new TCHAR[255];
 memset(szResult, 0x00, sizeof(szResult));
 GetPrivateProfileString(szSection,  szKey, szDefaultValue, szResult, 255, m_szFileName); 
 return szResult;
}

IniWriter.h

C++
//// Source: http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File ////

#ifndef INIWRITER_H
#define INIWRITER_H

#include <windows.h>

class CIniWriter
{
public:
 CIniWriter(LPCTSTR szFileName); 
 void WriteInteger(LPCTSTR szSection, LPCTSTR szKey, int iValue);
 void WriteFloat(LPCTSTR szSection, LPCTSTR szKey, float fltValue);
 void WriteBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolValue);
 void WriteString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szValue);
private:
 TCHAR m_szFileName[255];
};
#endif //INIWRITER_H

IniWriter.cpp

C++
//// Source: http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File ////

#include "IniWriter.h"
#include <iostream>
#include <windows.h> 

CIniWriter::CIniWriter(LPCTSTR szFileName)
{
 memset(m_szFileName, 0x00, sizeof(m_szFileName));
 memcpy(m_szFileName, szFileName, _tcslen(szFileName)*sizeof(TCHAR));
}
void CIniWriter::WriteInteger(LPCTSTR szSection, LPCTSTR szKey, int iValue)
{
 TCHAR szValue[255];
 _stprintf_s(szValue, 255, TEXT("%d"), iValue);
 WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName); 
}
void CIniWriter::WriteFloat(LPCTSTR szSection, LPCTSTR szKey, float fltValue)
{
 TCHAR szValue[255];
 _stprintf_s(szValue, 255, TEXT("%f"), fltValue);
 WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName); 
}
void CIniWriter::WriteBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolValue)
{
 TCHAR szValue[255];
 _stprintf_s(szValue, 255, TEXT("%s"), bolValue ? TEXT("True") : TEXT("False"));
 WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName); 
}
void CIniWriter::WriteString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szValue)
{
 WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName);
}

main.cpp 

C++
#if defined(UNICODE) || defined(_UNICODE)
#define tcout std::wcout
#else
#define tcout std::cout
#endif

#include <iostream>
#include <windows.h>
#include "IniWriter.h"
#include "IniReader.h"

int _tmain(int argc, _TCHAR* argv[])
{
 CIniWriter iniWriter(TEXT(".\\initest.ini"));
 iniWriter.WriteString(TEXT("Setting"), TEXT("Name"), TEXT("jianxx"));   
 iniWriter.WriteInteger(TEXT("Setting"), TEXT("Age"), 27); 
 iniWriter.WriteFloat(TEXT("Setting"), TEXT("Height"), 1.82f); 
 iniWriter.WriteBoolean(TEXT("Setting"), TEXT("Marriage"), false);  
 CIniReader iniReader(TEXT(".\\initest.ini"));
 LPTSTR szName = iniReader.ReadString(TEXT("Setting"), TEXT("Name"), TEXT(""));   
 int iAge = iniReader.ReadInteger(TEXT("Setting"), TEXT("Age"), 25); 
 float fltHieght = iniReader.ReadFloat(TEXT("Setting"), TEXT("Height"), 1.80f); 
 bool bMarriage = iniReader.ReadBoolean(TEXT("Setting"), TEXT("Marriage"), true); 
 
 tcout<<"Name:"<<szName<<std::endl
   <<"Age:"<<iAge<<std::endl 
   <<"Height:"<<fltHieght<<std::endl 
   <<"Marriage:"<<bMarriage<<std::endl; 
 delete szName;  
 return 1;   
}

History

  22th April, 2012: Initial post 

License

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


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalwell, could be better Pin
kamelkom14-May-13 1:17
kamelkom14-May-13 1:17 
GeneralMy vote of 3 Pin
Aescleal17-May-12 21:27
Aescleal17-May-12 21:27 
Two big problems with this lot:

- Someone can crash their program with it. Use std::basic_string of even MFC CString instead and the problem goes away. AND it makes your code far simpler.

- Don't use C functions everywhere. Everything you're doing in C you can do far easier in C ++

I'd also:

- stick them all in one class (ini_file, or CIniFile if you like MS's naming convention). If anything CIniFileReader/Writer are interfaces that CIniFile implements. If you do that you can reimplement your interfaces later as CRegistryKey or something to do the same thing.

- template it on the character type you want to use. Then you can use multiple types of character in the same application (which can be really handy when you're using UTF-16 or UCS4 internally and UTF-8 externally).

Sorry to sound so harsh but I really hate code that can crash when it needn't - especially when it's code I wrote. I also think that code that looks like it was written in 1993 when it wasn't isn't doing a lot for new C++ programmers who might think this is the way to be programming in 2012.

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.