Visual Basic, C & C++
분류 C & C++

c++ ini 파일 작성 쉽게하기

페이지 정보

본문

출처 : https://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File



A Small Class to Read INI File


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)

관련자료

등록된 댓글이 없습니다.
프로그래밍
Today's proverb
가을에 피는 국화는 첫 봄의 상징으로 사랑받는 개나리를 시샘하지 않는다. 역시 봄에 피는 복숭아꽃이나 벚꽃을 부러워하지 않는다. 한여름 붉은 장미가 필 때, 나는 왜 이렇게 다른 꽃보다 늦게 피나 한탄하지도 않는다. 그저 묵묵히 준비하며 내공을 쌓고 있을 뿐이다. 그러다가 매미소리 그치고 하늘이 높아지는 가을, 드디어 자기 차례가 돌아온 지금, 국화는 오랫동안 준비해온 그 은은한 향기와 자태를 마음껏 뽐내는 것이다. (한비야)