代码] 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 |
08 | class CIniReader |
09 | { |
10 | public: |
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); |
16 | private: |
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 |
07 | CIniReader::CIniReader(LPCTSTR
szFileName) |
08 | { |
09 | memset(m_szFileName, 0x00,
sizeof(m_szFileName)); |
10 | memcpy(m_szFileName, szFileName, _tcslen(szFileName)*sizeof(TCHAR)); |
11 | } |
12 | int CIniReader::ReadInteger(LPCTSTR
szSection, LPCTSTR
szKey, int iDefaultValue) |
13 | { |
14 | int
iResult = GetPrivateProfileInt(szSection, szKey, iDefaultValue, m_szFileName);
|
15 | return
iResult; |
16 | } |
17 | float 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 | } |
27 | bool 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 | } |
38 | LPTSTR 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 |
08 | class CIniWriter |
09 | { |
10 | public: |
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); |
16 | private: |
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 |
07 | CIniWriter::CIniWriter(LPCTSTR
szFileName) |
08 | { |
09 | memset(m_szFileName, 0x00,
sizeof(m_szFileName)); |
10 | memcpy(m_szFileName, szFileName, _tcslen(szFileName)*sizeof(TCHAR)); |
11 | } |
12 | void 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 | } |
18 | void 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 | } |
24 | void 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 | } |
30 | void 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 |
12 | int _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 | } |
本文介绍了一个用于读取和写入INI文件的C++类,包括读取整数、浮点数、布尔值和字符串的功能。
2453

被折叠的 条评论
为什么被折叠?



