C++读写INI配置文件的类实例
本文实例讲述了C++读写INI配置文件的类。分享给大家供大家参考。具体如下:
1.IniReader.h文件:
#ifndefINIREADER_H #defineINIREADER_H #include<windows.h> classCIniReader { public: CIniReader(LPCTSTRszFileName); intReadInteger(LPCTSTRszSection,LPCTSTRszKey,intiDefaultValue); floatReadFloat(LPCTSTRszSection,LPCTSTRszKey,floatfltDefaultValue); boolReadBoolean(LPCTSTRszSection,LPCTSTRszKey,boolbolDefaultValue); LPTSTRReadString(LPCTSTRszSection,LPCTSTRszKey,LPCTSTRszDefaultValue); private: TCHARm_szFileName[255]; }; #endif//INIREADER_H
2.IniReader.cpp文件:
#include"IniReader.h" #include<iostream> #include<windows.h> CIniReader::CIniReader(LPCTSTRszFileName) { memset(m_szFileName,0x00,sizeof(m_szFileName)); memcpy(m_szFileName,szFileName,_tcslen(szFileName)*sizeof(TCHAR)); } intCIniReader::ReadInteger(LPCTSTRszSection,LPCTSTRszKey,intiDefaultValue) { intiResult=GetPrivateProfileInt(szSection,szKey,iDefaultValue,m_szFileName); returniResult; } floatCIniReader::ReadFloat(LPCTSTRszSection,LPCTSTRszKey,floatfltDefaultValue) { TCHARszResult[255]; TCHARszDefault[255]; floatfltResult; _stprintf_s(szDefault,255,TEXT("%f"),fltDefaultValue); GetPrivateProfileString(szSection,szKey,szDefault,szResult,255,m_szFileName); fltResult=(float)_tstof(szResult); returnfltResult; } boolCIniReader::ReadBoolean(LPCTSTRszSection,LPCTSTRszKey,boolbolDefaultValue) { TCHARszResult[255]; TCHARszDefault[255]; boolbolResult; _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; returnbolResult; } LPTSTRCIniReader::ReadString(LPCTSTRszSection,LPCTSTRszKey,LPCTSTRszDefaultValue) { LPTSTRszResult=newTCHAR[255]; memset(szResult,0x00,sizeof(szResult)); GetPrivateProfileString(szSection,szKey,szDefaultValue,szResult,255,m_szFileName); returnszResult; }
3.IniWriter.h文件:
#ifndefINIWRITER_H #defineINIWRITER_H #include<windows.h> classCIniWriter { public: CIniWriter(LPCTSTRszFileName); voidWriteInteger(LPCTSTRszSection,LPCTSTRszKey,intiValue); voidWriteFloat(LPCTSTRszSection,LPCTSTRszKey,floatfltValue); voidWriteBoolean(LPCTSTRszSection,LPCTSTRszKey,boolbolValue); voidWriteString(LPCTSTRszSection,LPCTSTRszKey,LPCTSTRszValue); private: TCHARm_szFileName[255]; }; #endif//INIWRITER_H
4.IniWriter.cpp文件:
#include"IniWriter.h" #include<iostream> #include<windows.h> CIniWriter::CIniWriter(LPCTSTRszFileName) { memset(m_szFileName,0x00,sizeof(m_szFileName)); memcpy(m_szFileName,szFileName,_tcslen(szFileName)*sizeof(TCHAR)); } voidCIniWriter::WriteInteger(LPCTSTRszSection,LPCTSTRszKey,intiValue) { TCHARszValue[255]; _stprintf_s(szValue,255,TEXT("%d"),iValue); WritePrivateProfileString(szSection,szKey,szValue,m_szFileName); } voidCIniWriter::WriteFloat(LPCTSTRszSection,LPCTSTRszKey,floatfltValue) { TCHARszValue[255]; _stprintf_s(szValue,255,TEXT("%f"),fltValue); WritePrivateProfileString(szSection,szKey,szValue,m_szFileName); } voidCIniWriter::WriteBoolean(LPCTSTRszSection,LPCTSTRszKey,boolbolValue) { TCHARszValue[255]; _stprintf_s(szValue,255,TEXT("%s"),bolValue?TEXT("True"):TEXT("False")); WritePrivateProfileString(szSection,szKey,szValue,m_szFileName); } voidCIniWriter::WriteString(LPCTSTRszSection,LPCTSTRszKey,LPCTSTRszValue) { WritePrivateProfileString(szSection,szKey,szValue,m_szFileName); }
5.main.cpp文件:
#ifdefined(UNICODE)||defined(_UNICODE) #definetcoutstd::wcout #else #definetcoutstd::cout #endif #include<iostream> #include<windows.h> #include"IniWriter.h" #include"IniReader.h" int_tmain(intargc,_TCHAR*argv[]) { CIniWriteriniWriter(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); CIniReaderiniReader(TEXT(".\\initest.ini")); LPTSTRszName=iniReader.ReadString(TEXT("Setting"),TEXT("Name"),TEXT("")); intiAge=iniReader.ReadInteger(TEXT("Setting"),TEXT("Age"),25); floatfltHieght=iniReader.ReadFloat(TEXT("Setting"),TEXT("Height"),1.80f); boolbMarriage=iniReader.ReadBoolean(TEXT("Setting"),TEXT("Marriage"),true); tcout<<"Name:"<<szName<<std::endl <<"Age:"<<iAge<<std::endl <<"Height:"<<fltHieght<<std::endl <<"Marriage:"<<bMarriage<<std::endl; deleteszName; return1; }
希望本文所述对大家的C++程序设计有所帮助。