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

C++读写INI配置文件的类实例,第1张

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

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

1. IniReader.h文件:

#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,float fltDefaultValue); bool ReadBoolean(LPCTSTR szSection,bool bolDefaultValue); LPTSTR ReadString(LPCTSTR szSection,LPCTSTR szDefaultValue);private: TCHAR m_szfilename[255];};#endif //INIREADER_H

2. IniReader.cpp文件:

#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,int IDefaultValue){ int iResult = GetPrivateProfileInt(szSection,szKey,IDefaultValue,m_szfilename);  return iResult;}float CIniReader::Readfloat(LPCTSTR szSection,float fltDefaultValue){ TCHAR szResult[255]; TCHAR szDefault[255]; float fltResult; _stprintf_s(szDefault,255,TEXT("%f"),fltDefaultValue); GetPrivateProfileString(szSection,szDefault,szResult,m_szfilename);  fltResult = (float)_tstof(szResult); return fltResult;}bool CIniReader::ReadBoolean(LPCTSTR szSection,bool bolDefaultValue){ TCHAR szResult[255]; TCHAR szDefault[255]; bool bolResult; _stprintf_s(szDefault,TEXT("%s"),bolDefaultValue? TEXT("True") : TEXT("False")); GetPrivateProfileString(szSection,m_szfilename);  bolResult = (_tcscmp(szResult,TEXT("True")) == 0 ||     _tcscmp(szResult,TEXT("true")) == 0) ? true : false; return bolResult;}LPTSTR CIniReader::ReadString(LPCTSTR szSection,LPCTSTR szDefaultValue){ LPTSTR szResult = new TCHAR[255]; memset(szResult,sizeof(szResult)); GetPrivateProfileString(szSection,szDefaultValue,m_szfilename);  return szResult;}

3. IniWriter.h文件:

#ifndef INIWRITER_H#define INIWRITER_H#include <windows.h>class CIniWriter{public: CIniWriter(LPCTSTR szfilename);  voID WriteInteger(LPCTSTR szSection,int iValue); voID Writefloat(LPCTSTR szSection,float fltValue); voID WriteBoolean(LPCTSTR szSection,bool bolValue); voID WriteString(LPCTSTR szSection,LPCTSTR szValue);private: TCHAR m_szfilename[255];};#endif //INIWRITER_H

4. IniWriter.cpp文件:

#include "IniWriter.h"#include <iostream>#include <windows.h> CIniWriter::CIniWriter(LPCTSTR szfilename){ memset(m_szfilename,_tcslen(szfilename)*sizeof(TCHAR));}voID CIniWriter::WriteInteger(LPCTSTR szSection,int iValue){ TCHAR szValue[255]; _stprintf_s(szValue,TEXT("%d"),iValue); WritePrivateProfileString(szSection,szValue,m_szfilename); }voID CIniWriter::Writefloat(LPCTSTR szSection,float fltValue){ TCHAR szValue[255]; _stprintf_s(szValue,fltValue); WritePrivateProfileString(szSection,m_szfilename); }voID CIniWriter::WriteBoolean(LPCTSTR szSection,bool bolValue){ TCHAR szValue[255]; _stprintf_s(szValue,bolValue ? TEXT("True") : TEXT("False")); WritePrivateProfileString(szSection,m_szfilename); }voID CIniWriter::WriteString(LPCTSTR szSection,LPCTSTR szValue){ WritePrivateProfileString(szSection,m_szfilename);}

5. main.cpp文件:

#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(""));   int iAge = iniReader.ReadInteger(TEXT("Setting"),25);  float fltHIEght = iniReader.Readfloat(TEXT("Setting"),1.80f);  bool bMarriage = iniReader.ReadBoolean(TEXT("Setting"),true);  tcout<<"name:"<<szname<<std::endl  <<"Age:"<<iAge<<std::endl   <<"Height:"<<fltHIEght<<std::endl   <<"Marriage:"<<bMarriage<<std::endl;  delete szname;  return 1;  }

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

总结

以上是内存溢出为你收集整理的C++读写INI配置文件的类实例全部内容,希望文章能够帮你解决C++读写INI配置文件的类实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1256360.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存