C++读写INI配置文件的类实例

2025-05-29 0 54

本文实例讲述了C++读写INI配置文件的类。分享给大家供大家参考。具体如下:

1. IniReader.h文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
#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

2. IniReader.cpp文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41
#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;

}

3. IniWriter.h文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
#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

4. IniWriter.cpp文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30
#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);

}

5. main.cpp文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28
#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;

}

希望本文所述对大家的C++程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 C++读写INI配置文件的类实例 https://www.kuaiidc.com/107847.html

相关文章

发表评论
暂无评论