用于读写 INI 配置文件的 C++ 类

代码] IniReader.h

01 Source: http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File
02 
03#ifndef INIREADER_H
04#define INIREADER_H
05 
06#include <windows.h>
07 
08class CIniReader
09{
10public:
11 CIniReader(LPCTSTR szFileName);
12 int ReadInteger(LPCTSTR szSection, LPCTSTR szKey, int iDefaultValue);
13 float ReadFloat(LPCTSTR szSection, LPCTSTR szKey, float fltDefaultValue);
14 bool ReadBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolDefaultValue);
15 LPTSTR ReadString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szDefaultValue);
16private:
17  TCHAR m_szFileName[255];
18};
19#endif //INIREADER_H

[代码] IniReader.cpp

01 Source: http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File
02 
03#include "IniReader.h"
04#include <iostream>
05#include <windows.h>
06 
07CIniReader::CIniReader(LPCTSTR szFileName)
08{
09 memset(m_szFileName, 0x00, sizeof(m_szFileName));
10 memcpy(m_szFileName, szFileName, _tcslen(szFileName)*sizeof(TCHAR));
11}
12int CIniReader::ReadInteger(LPCTSTR szSection, LPCTSTR szKey, int iDefaultValue)
13{
14 int iResult = GetPrivateProfileInt(szSection,  szKey, iDefaultValue, m_szFileName);
15 return iResult;
16}
17float CIniReader::ReadFloat(LPCTSTR szSection, LPCTSTR szKey, float fltDefaultValue)
18{
19 TCHAR szResult[255];
20 TCHAR szDefault[255];
21 float fltResult;
22 _stprintf_s(szDefault, 255, TEXT("%f"),fltDefaultValue);
23 GetPrivateProfileString(szSection,  szKey, szDefault, szResult, 255, m_szFileName);
24 fltResult =  (float)_tstof(szResult);
25 return fltResult;
26}
27bool CIniReader::ReadBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolDefaultValue)
28{
29 TCHAR szResult[255];
30 TCHAR szDefault[255];
31 bool bolResult;
32 _stprintf_s(szDefault, 255, TEXT("%s"), bolDefaultValue? TEXT("True") : TEXT("False"));
33 GetPrivateProfileString(szSection, szKey, szDefault, szResult, 255, m_szFileName);
34 bolResult =  (_tcscmp(szResult, TEXT("True")) == 0 ||
35        _tcscmp(szResult, TEXT("true")) == 0) ? true : false;
36 return bolResult;
37}
38LPTSTR CIniReader::ReadString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szDefaultValue)
39{
40 LPTSTR szResult = new TCHAR[255];
41 memset(szResult, 0x00, sizeof(szResult));
42 GetPrivateProfileString(szSection,  szKey, szDefaultValue, szResult, 255, m_szFileName);
43 return szResult;
44}

[代码] IniWriter.h

01 Source: http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File
02 
03#ifndef INIWRITER_H
04#define INIWRITER_H
05 
06#include <windows.h>
07 
08class CIniWriter
09{
10public:
11 CIniWriter(LPCTSTR szFileName);
12 void WriteInteger(LPCTSTR szSection, LPCTSTR szKey, int iValue);
13 void WriteFloat(LPCTSTR szSection, LPCTSTR szKey, float fltValue);
14 void WriteBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolValue);
15 void WriteString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szValue);
16private:
17 TCHAR m_szFileName[255];
18};
19#endif //INIWRITER_H

[代码] IniWriter.cpp

01 Source: http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File
02 
03#include "IniWriter.h"
04#include <iostream>
05#include <windows.h>
06 
07CIniWriter::CIniWriter(LPCTSTR szFileName)
08{
09 memset(m_szFileName, 0x00, sizeof(m_szFileName));
10 memcpy(m_szFileName, szFileName, _tcslen(szFileName)*sizeof(TCHAR));
11}
12void CIniWriter::WriteInteger(LPCTSTR szSection, LPCTSTR szKey, int iValue)
13{
14 TCHAR szValue[255];
15 _stprintf_s(szValue, 255, TEXT("%d"), iValue);
16 WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName);
17}
18void CIniWriter::WriteFloat(LPCTSTR szSection, LPCTSTR szKey, float fltValue)
19{
20 TCHAR szValue[255];
21 _stprintf_s(szValue, 255, TEXT("%f"), fltValue);
22 WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName);
23}
24void CIniWriter::WriteBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolValue)
25{
26 TCHAR szValue[255];
27 _stprintf_s(szValue, 255, TEXT("%s"), bolValue ? TEXT("True") : TEXT("False"));
28 WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName);
29}
30void CIniWriter::WriteString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szValue)
31{
32 WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName);
33}

[代码] main.cpp

01#if defined(UNICODE) || defined(_UNICODE)
02#define tcout std::wcout
03#else
04#define tcout std::cout
05#endif
06 
07#include <iostream>
08#include <windows.h>
09#include "IniWriter.h"
10#include "IniReader.h"
11 
12int _tmain(int argc, _TCHAR* argv[])
13{
14 CIniWriter iniWriter(TEXT(".\\initest.ini"));
15 iniWriter.WriteString(TEXT("Setting"), TEXT("Name"), TEXT("jianxx"));  
16 iniWriter.WriteInteger(TEXT("Setting"), TEXT("Age"), 27);
17 iniWriter.WriteFloat(TEXT("Setting"), TEXT("Height"), 1.82f);
18 iniWriter.WriteBoolean(TEXT("Setting"), TEXT("Marriage"), false); 
19 CIniReader iniReader(TEXT(".\\initest.ini"));
20 LPTSTR szName = iniReader.ReadString(TEXT("Setting"), TEXT("Name"), TEXT(""));  
21 int iAge = iniReader.ReadInteger(TEXT("Setting"), TEXT("Age"), 25);
22 float fltHieght = iniReader.ReadFloat(TEXT("Setting"), TEXT("Height"), 1.80f);
23 bool bMarriage = iniReader.ReadBoolean(TEXT("Setting"), TEXT("Marriage"), true);
24  
25 tcout<<"Name:"<<szName<<std::endl
26   <<"Age:"<<iAge<<std::endl
27   <<"Height:"<<fltHieght<<std::endl
28   <<"Marriage:"<<bMarriage<<std::endl;
29 delete szName; 
30 return 1;  
31}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值