博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
操作INI文件的读写类
阅读量:4035 次
发布时间:2019-05-24

本文共 4884 字,大约阅读时间需要 16 分钟。

http://www.codeproject.com/Tips/376146/A-Small-Class-to-Read-INI-Files

IniReader.h

Source: http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File #ifndef INIREADER_H#define INIREADER_H#include 
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

IniReader.cpp

Source: http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File #include "IniReader.h"#include 
#include
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;}

IniWriter.h

Source: http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File #ifndef INIWRITER_H#define INIWRITER_H#include 
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

IniWriter.cpp

Source: http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File #include "IniWriter.h"#include 
#include
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);}

main.cpp

#if defined(UNICODE) || defined(_UNICODE)#define tcout std::wcout#else#define tcout std::cout#endif#include 
#include
#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:"<
<
<<"Age:"<
<
<<"Height:"<
<
<<"Marriage:"<
<

转载地址:http://jofdi.baihongyu.com/

你可能感兴趣的文章
Search in Rotated Sorted Array II
查看>>
Sqrt(x)
查看>>
Search for a Range
查看>>
Valid Palindrome
查看>>
Word Break
查看>>
Surrounded Regions
查看>>
Largest Rectangle in Histogram
查看>>
免费馅饼
查看>>
Common Subsequence
查看>>
Humble Numbers
查看>>
Merge Intervals
查看>>
Insert Interval
查看>>
Trapping Rain Water
查看>>
Pow(x, n)
查看>>
Sort Colors
查看>>
Restore IP Addresses
查看>>
Word Break II
查看>>
Anagrams
查看>>
Letter Combinations of a Phone Number
查看>>
Combination Sum
查看>>