C++/ini Reader Writer Edit Diff Refresh Backlink Random Search History Help Setting Hide Show Contents 1. Code 2. Usage Origin at http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File Quick Start 1. Code # IniReader.h view plain#ifndef INIREADER_H#define INIREADER_Hclass 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#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 IniWriter.h view plain#ifndef INIWRITER_H#define INIWRITER_Hclass 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#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 IniReader.cpp view plain#include "stdafx.h"#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;}#include "stdafx.h" #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.cpp view plain#include "stdafx.h"#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);}#include "stdafx.h" #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); } files.zip (1.71 KB) 2. Usage # ; config.ini [Setting] Age=19 view plainCIniReader iniReader(".\\config.ini");// ReadInteger(/* Section */, /* Key(Property), /* Default Value */)int age = iniReader.ReadInteger("Setting", "Age", 1);CIniWriter iniWriter(".\\config.ini");iniWriter.WriteString("Setting", "Name", "myname");CIniReader iniReader(".\\config.ini"); // ReadInteger(/* Section */, /* Key(Property), /* Default Value */) int age = iniReader.ReadInteger("Setting", "Age", 1); CIniWriter iniWriter(".\\config.ini"); iniWriter.WriteString("Setting", "Name", "myname"); 이 글에는 0 개의 댓글이 있습니다. Please enable JavaScript to view the comments powered by Disqus.