热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

(vc)分享一个读写ini文件的类,支持多种数据类型的读写,二进制数据都能保存和读取...

读写ini文件的类叫CIni,有ini.h和ini.cpp两个文件组成。ini.h文件:#pragmaonce#defineSER_GET(bGet,value)Se

读写ini文件的类叫CIni,有ini.h和ini.cpp两个文件组成。

 

 

ini.h文件:

#pragma once#define SER_GET(bGet,value) SerGet(bGet,value,#value)
#define SER_ARR(bGet,value,n) SerGet(bGet,value,n,#value)
#define SER_GETD(bGet,value,default) SerGet(bGet,value,#value,NULL,default)
#define SER_ARRD(bGet,value,n,default) SerGet(bGet,value,n,#value,default)class CIni
{
public:// If the IniFilename contains no path,// the module-directory will be add to the FileName,// to avoid storing in the windows-directory// bModulPath=true: ModulDir, bModulPath=false: CurrentDirstatic void AddModulPath(CString& rstrFileName, bool bModulPath = true);static CString GetDefaultSection();static CString GetDefaultIniFile(bool bModulPath = true);CIni();CIni(CIni const& rIni);CIni(CString const& rstrFileName);CIni(CString const& rstrFileName, CString const& rstrSection);virtual ~CIni();void SetFileName(const CString& rstrFileName);void SetSection(const CString& rstrSection);const CString& GetFileName() const;const CString& GetSection() const;CString GetString(LPCTSTR lpszEntry, LPCTSTR lpszDefault = NULL, LPCTSTR lpszSection = NULL);CString GetStringUTF8(LPCTSTR lpszEntry,LPCTSTR lpszDefault = NULL, LPCTSTR lpszSection = NULL);CString GetStringLong(LPCTSTR lpszEntry,LPCTSTR lpszDefault = NULL, LPCTSTR lpszSection = NULL);double GetDouble(LPCTSTR lpszEntry, double fDefault = 0.0, LPCTSTR lpszSection = NULL);float GetFloat(LPCTSTR lpszEntry, float fDefault = 0.0F, LPCTSTR lpszSection = NULL);int GetInt(LPCTSTR lpszEntry, int nDefault = 0, LPCTSTR lpszSection = NULL);ULONGLONG GetUInt64(LPCTSTR lpszEntry, ULONGLONG nDefault = 0, LPCTSTR lpszSection = NULL);WORD GetWORD(LPCTSTR lpszEntry, WORD nDefault = 0, LPCTSTR lpszSection = NULL);bool GetBool(LPCTSTR lpszEntry, bool bDefault = false, LPCTSTR lpszSection = NULL);CPoint GetPoint(LPCTSTR lpszEntry, CPoint ptDefault = CPoint(0,0), LPCTSTR lpszSection = NULL);CRect GetRect(LPCTSTR lpszEntry, CRect rectDefault = CRect(0,0,0,0), LPCTSTR lpszSection = NULL);COLORREF GetColRef(LPCTSTR lpszEntry, COLORREF crDefault = RGB(128,128,128), LPCTSTR lpszSection = NULL);bool GetBinary(LPCTSTR lpszEntry, BYTE** ppData, UINT* pBytes, LPCTSTR lpszSection = NULL);void WriteString(LPCTSTR lpszEntry, LPCTSTR s, LPCTSTR lpszSection = NULL);void WriteStringUTF8(LPCTSTR lpszEntry,LPCTSTR s, LPCTSTR lpszSection = NULL);void WriteDouble(LPCTSTR lpszEntry, double f, LPCTSTR lpszSection = NULL);void WriteFloat(LPCTSTR lpszEntry, float f, LPCTSTR lpszSection = NULL);void WriteInt(LPCTSTR lpszEntry, int n, LPCTSTR lpszSection = NULL);void WriteUInt64(LPCTSTR lpszEntry, ULONGLONG n, LPCTSTR lpszSection = NULL);void WriteWORD(LPCTSTR lpszEntry, WORD n, LPCTSTR lpszSection = NULL);void WriteBool(LPCTSTR lpszEntry, bool b, LPCTSTR lpszSection = NULL);void WritePoint(LPCTSTR lpszEntry, CPoint pt, LPCTSTR lpszSection = NULL);void WriteRect(LPCTSTR lpszEntry, CRect rect, LPCTSTR lpszSection = NULL);void WriteColRef(LPCTSTR lpszEntry, COLORREF cr, LPCTSTR lpszSection = NULL);bool WriteBinary(LPCTSTR lpszEntry, LPBYTE pData, UINT nBytes, LPCTSTR lpszSection = NULL);void SerGetString( bool bGet, CString& s, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, LPCTSTR lpszDefault = NULL);void SerGetDouble( bool bGet, double& f, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, double fDefault = 0.0);void SerGetFloat( bool bGet, float& f, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, float fDefault = 0.0);void SerGetInt( bool bGet, int& n, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, int nDefault = 0);void SerGetDWORD( bool bGet, DWORD& n, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, DWORD nDefault = 0);void SerGetBool( bool bGet, bool& b, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, bool bDefault = false);void SerGetPoint( bool bGet, CPoint& pt, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, CPoint ptDefault = CPoint(0,0));void SerGetRect( bool bGet, CRect& rc, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, CRect rectDefault = CRect(0,0,0,0));void SerGetColRef( bool bGet, COLORREF& cr, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, COLORREF crDefault = RGB(128,128,128));void SerGet( bool bGet, CString& s, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, LPCTSTR lpszDefault = NULL);void SerGet( bool bGet, double& f, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, double fDefault = 0.0);void SerGet( bool bGet, float& f, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, float fDefault = 0.0F);void SerGet( bool bGet, int& n, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, int nDefault = 0);void SerGet( bool bGet, short& n, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, int nDefault = 0);void SerGet( bool bGet, DWORD& n, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, DWORD nDefault = 0);void SerGet( bool bGet, WORD& n, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, DWORD nDefault = 0);void SerGet( bool bGet, CPoint& pt, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, CPoint ptDefault = CPoint(0,0));void SerGet( bool bGet, CRect& rc, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, CRect rectDefault = CRect(0,0,0,0));void SerGet( bool bGet, CString* s, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, LPCTSTR lpszDefault = NULL);void SerGet( bool bGet, double* f, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, double fDefault = 0.0);void SerGet( bool bGet, float* f, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, float fDefault = 0.0F);void SerGet( bool bGet, BYTE* n, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, BYTE nDefault = 0);void SerGet( bool bGet, int* n, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, int nDefault = 0);void SerGet( bool bGet, short* n, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, int nDefault = 0);void SerGet( bool bGet, DWORD* n, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, DWORD nDefault = 0);void SerGet( bool bGet, WORD* n, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, DWORD nDefault = 0);void SerGet( bool bGet, CPoint* pt, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, CPoint ptDefault = CPoint(0,0));void SerGet( bool bGet, CRect* rc, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection = NULL, CRect rectDefault = CRect(0,0,0,0));int Parse(const CString&, int nOffset, CString &rstrOut);void DeleteKey(LPCTSTR lpszKey);private:void Init(LPCTSTR lpszIniFile, LPCTSTR lpszSection = NULL);LPTSTR GetLPCSTR(LPCTSTR lpszEntry, LPCTSTR lpszSection, LPCTSTR lpszDefault);bool m_bModulPath; //true: Filenames without path take the Modulepath//false: Filenames without path take the CurrentDirectory#define MAX_INI_BUFFER 256TCHAR m_chBuffer[MAX_INI_BUFFER];CString m_strFileName;CString m_strSection;static CString Read( LPCTSTR lpszFileName, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault);static void Write(LPCTSTR lpszFileName, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue);
};

 

 

 

ini.cpp文件:

#include "StdAfx.h"
#include
#include
#include ".\ini.h"
#include "StringConversion.h"#ifndef _countof
#define _countof(array) (sizeof(array)/sizeof(array[0]))
#endifvoid CIni::AddModulPath(CString &rstrFileName, bool bModulPath)
{TCHAR drive[_MAX_DRIVE];TCHAR dir[_MAX_DIR];TCHAR fname[_MAX_FNAME];TCHAR ext[_MAX_EXT];_tsplitpath(rstrFileName, drive, dir, fname, ext);if (!drive[0]){//PathCanonicalize(..) doesn't work with for all Plattforms !CString strModule;if (bModulPath){DWORD dwModPathLen = GetModuleFileName(NULL, strModule.GetBuffer(MAX_PATH), MAX_PATH);strModule.ReleaseBuffer((dwModPathLen == 0 || dwModPathLen == MAX_PATH) ? 0 : -1);}else{DWORD dwCurDirLen = GetCurrentDirectory(MAX_PATH, strModule.GetBuffer(MAX_PATH));strModule.ReleaseBuffer((dwCurDirLen == 0 || dwCurDirLen >= MAX_PATH) ? 0 : -1);strModule.TrimRight(_T('\\'));strModule.TrimRight(_T('/'));strModule += _T("\\");}_tsplitpath(strModule, drive, dir, fname, ext);strModule = drive;strModule += dir;strModule += rstrFileName;rstrFileName = strModule;}
}CString CIni::GetDefaultSection()
{return AfxGetAppName();
}CString CIni::GetDefaultIniFile(bool bModulPath)
{TCHAR drive[_MAX_DRIVE];TCHAR dir[_MAX_DIR];TCHAR fname[_MAX_FNAME];TCHAR ext[_MAX_EXT];CString strTemp;CString strApplName;DWORD dwModPathLen = GetModuleFileName(NULL, strTemp.GetBuffer(MAX_PATH), MAX_PATH);strTemp.ReleaseBuffer((dwModPathLen == 0 || dwModPathLen == MAX_PATH) ? 0 : -1);_tsplitpath( strTemp, drive, dir, fname, ext );strTemp = fname;strTemp += _T(".ini");if (bModulPath){strApplName = drive;strApplName += dir;strApplName += strTemp;}else{DWORD dwCurDirLen = GetCurrentDirectory(MAX_PATH, strApplName.GetBuffer(MAX_PATH));strApplName.ReleaseBuffer((dwCurDirLen == 0 || dwCurDirLen >= MAX_PATH) ? 0 : -1);strApplName.TrimRight(_T('\\'));strApplName.TrimRight(_T('/'));strApplName += _T("\\");strApplName += strTemp;}return strApplName;
}CIni::CIni():
m_bModulPath(true)
{m_strFileName = GetDefaultIniFile(m_bModulPath);m_strSection = GetDefaultSection();
}CIni::CIni(CIni const &Ini):
m_strFileName(Ini.m_strFileName),
m_strSection(Ini.m_strSection),
m_bModulPath(Ini.m_bModulPath)
{if (m_strFileName.IsEmpty())m_strFileName = GetDefaultIniFile(m_bModulPath);AddModulPath(m_strFileName, m_bModulPath);if (m_strSection.IsEmpty())m_strSection = GetDefaultSection();
}CIni::CIni(CString const &rstrFileName):
m_strFileName(rstrFileName),
m_bModulPath(true)
{if (m_strFileName.IsEmpty())m_strFileName = GetDefaultIniFile(m_bModulPath);AddModulPath(m_strFileName, m_bModulPath);m_strSection = GetDefaultSection();
}CIni::CIni(CString const &rstrFileName, CString const &rstrSection):
m_strFileName(rstrFileName),
m_strSection(rstrSection),
m_bModulPath(true)
{if (m_strFileName.IsEmpty())m_strFileName = GetDefaultIniFile(m_bModulPath);AddModulPath(m_strFileName, m_bModulPath);if (m_strSection.IsEmpty())m_strSection = GetDefaultSection();
}CIni::~CIni()
{
}void CIni::SetFileName(const CString &rstrFileName)
{m_strFileName = rstrFileName;AddModulPath(m_strFileName);
}void CIni::SetSection(const CString &rstrSection)
{m_strSection = rstrSection;
}const CString& CIni::GetFileName() const
{return m_strFileName;
}const CString& CIni::GetSection() const
{return m_strSection;
}void CIni::Init(LPCTSTR lpszFileName, LPCTSTR lpszSection)
{if (lpszSection != NULL)m_strSection = lpszSection;if (lpszFileName != NULL)m_strFileName = lpszFileName;
}CString CIni::GetString(LPCTSTR lpszEntry, LPCTSTR lpszDefault, LPCTSTR lpszSection)
{if (lpszDefault == NULL)return GetLPCSTR(lpszEntry, lpszSection, _T(""));elsereturn GetLPCSTR(lpszEntry, lpszSection, lpszDefault);
}CString CIni::GetStringLong(LPCTSTR lpszEntry, LPCTSTR lpszDefault, LPCTSTR lpszSection)
{CString ret;unsigned int maxstrlen = MAX_INI_BUFFER;if (lpszSection != NULL)m_strSection = lpszSection;do {GetPrivateProfileString(m_strSection, lpszEntry, (lpszDefault == NULL) ? _T("") : lpszDefault, ret.GetBufferSetLength(maxstrlen), maxstrlen, m_strFileName);ret.ReleaseBuffer();if ((unsigned int)ret.GetLength() }CString CIni::GetStringUTF8(LPCTSTR lpszEntry, LPCTSTR lpszDefault, LPCTSTR lpszSection)
{if (lpszSection != NULL)m_strSection = lpszSection;CStringA strUTF8;GetPrivateProfileStringA(CT2CA(m_strSection), CT2CA(lpszEntry), CT2CA(lpszDefault),strUTF8.GetBufferSetLength(MAX_INI_BUFFER), MAX_INI_BUFFER, CT2CA(m_strFileName));strUTF8.ReleaseBuffer();return OptUtf8ToStr(strUTF8);
}double CIni::GetDouble(LPCTSTR lpszEntry, double fDefault, LPCTSTR lpszSection)
{TCHAR szDefault[MAX_PATH];_sntprintf(szDefault, _countof(szDefault), _T("%g"), fDefault);szDefault[_countof(szDefault) - 1] = _T('\0');GetLPCSTR(lpszEntry, lpszSection, szDefault);return _tstof(m_chBuffer);
}float CIni::GetFloat(LPCTSTR lpszEntry, float fDefault, LPCTSTR lpszSection)
{TCHAR szDefault[MAX_PATH];_sntprintf(szDefault, _countof(szDefault), _T("%g"), fDefault);szDefault[_countof(szDefault) - 1] = _T('\0');GetLPCSTR(lpszEntry, lpszSection, szDefault);return (float)_tstof(m_chBuffer);
}int CIni::GetInt(LPCTSTR lpszEntry, int nDefault, LPCTSTR lpszSection)
{TCHAR szDefault[MAX_PATH];_sntprintf(szDefault, _countof(szDefault), _T("%d"), nDefault);szDefault[_countof(szDefault) - 1] = _T('\0');GetLPCSTR(lpszEntry, lpszSection, szDefault);return _tstoi(m_chBuffer);
}ULONGLONG CIni::GetUInt64(LPCTSTR lpszEntry, ULONGLONG nDefault, LPCTSTR lpszSection)
{TCHAR szDefault[MAX_PATH];_sntprintf(szDefault, _countof(szDefault), _T("%I64u"), nDefault);szDefault[_countof(szDefault) - 1] = _T('\0');GetLPCSTR(lpszEntry, lpszSection, szDefault);ULONGLONG nResult;if (_stscanf(m_chBuffer, _T("%I64u"), &nResult) != 1)return nDefault;return nResult;
}WORD CIni::GetWORD(LPCTSTR lpszEntry, WORD nDefault, LPCTSTR lpszSection)
{TCHAR szDefault[MAX_PATH];_sntprintf(szDefault, _countof(szDefault), _T("%u"), nDefault);szDefault[_countof(szDefault) - 1] = _T('\0');GetLPCSTR(lpszEntry, lpszSection, szDefault);return (WORD)_tstoi(m_chBuffer);
}bool CIni::GetBool(LPCTSTR lpszEntry, bool bDefault, LPCTSTR lpszSection)
{TCHAR szDefault[MAX_PATH];_sntprintf(szDefault, _countof(szDefault), _T("%d"), bDefault);szDefault[_countof(szDefault) - 1] = _T('\0');GetLPCSTR(lpszEntry, lpszSection, szDefault);return _tstoi(m_chBuffer) != 0;
}CPoint CIni::GetPoint(LPCTSTR lpszEntry, CPoint ptDefault, LPCTSTR lpszSection)
{CPoint ptReturn = ptDefault;CString strDefault;strDefault.Format(_T("(%d,%d)"), ptDefault.x, ptDefault.y);CString strPoint = GetString(lpszEntry, strDefault, lpszSection);if (_stscanf(strPoint,_T("(%d,%d)"), &ptReturn.x, &ptReturn.y) != 2)return ptDefault;return ptReturn;
}CRect CIni::GetRect(LPCTSTR lpszEntry, CRect rectDefault, LPCTSTR lpszSection)
{CRect rectReturn = rectDefault;CString strDefault;strDefault.Format(_T("%d,%d,%d,%d"), rectDefault.left, rectDefault.top, rectDefault.right, rectDefault.bottom);CString strRect = GetString(lpszEntry, strDefault, lpszSection);//new Version foundif (_stscanf(strRect, _T("%d,%d,%d,%d"), &rectDefault.left, &rectDefault.top, &rectDefault.right, &rectDefault.bottom) == 4)return rectReturn;//old Version foundif (_stscanf(strRect, _T("(%d,%d,%d,%d)"), &rectReturn.top, &rectReturn.left, &rectReturn.bottom, &rectReturn.right) != 4)return rectDefault;return rectReturn;
}COLORREF CIni::GetColRef(LPCTSTR lpszEntry, COLORREF crDefault, LPCTSTR lpszSection)
{int temp[3] = { GetRValue(crDefault),GetGValue(crDefault),GetBValue(crDefault) };CString strDefault;strDefault.Format(_T("RGB(%hd,%hd,%hd)"), temp[0], temp[1], temp[2]);CString strColRef = GetString(lpszEntry, strDefault, lpszSection);if (_stscanf(strColRef, _T("RGB(%d,%d,%d)"), temp, temp+1, temp+2) != 3)return crDefault;return RGB(temp[0], temp[1], temp[2]);
}void CIni::WriteString(LPCTSTR lpszEntry, LPCTSTR lpsz, LPCTSTR lpszSection)
{if (lpszSection != NULL) m_strSection = lpszSection;WritePrivateProfileString(m_strSection, lpszEntry, lpsz, m_strFileName);
}void CIni::WriteStringUTF8(LPCTSTR lpszEntry, LPCTSTR lpsz, LPCTSTR lpszSection)
{if (lpszSection != NULL) m_strSection = lpszSection;CString str(lpsz);WritePrivateProfileStringA(CT2CA(m_strSection), CT2CA(lpszEntry), StrToUtf8(str), CT2CA(m_strFileName));
}void CIni::WriteDouble(LPCTSTR lpszEntry, double f, LPCTSTR lpszSection)
{if (lpszSection != NULL)m_strSection = lpszSection;TCHAR szBuffer[MAX_PATH];_sntprintf(szBuffer, _countof(szBuffer), _T("%g"), f);szBuffer[_countof(szBuffer) - 1] = _T('\0');WritePrivateProfileString(m_strSection, lpszEntry, szBuffer, m_strFileName);
}void CIni::WriteFloat(LPCTSTR lpszEntry, float f, LPCTSTR lpszSection)
{if (lpszSection != NULL)m_strSection = lpszSection;TCHAR szBuffer[MAX_PATH];_sntprintf(szBuffer, _countof(szBuffer), _T("%g"), f);szBuffer[_countof(szBuffer) - 1] = _T('\0');WritePrivateProfileString(m_strSection, lpszEntry, szBuffer, m_strFileName);
}void CIni::WriteInt(LPCTSTR lpszEntry, int n, LPCTSTR lpszSection)
{if (lpszSection != NULL)m_strSection = lpszSection;TCHAR szBuffer[MAX_PATH];_itot(n, szBuffer, 10);WritePrivateProfileString(m_strSection, lpszEntry, szBuffer, m_strFileName);
}void CIni::WriteUInt64(LPCTSTR lpszEntry, ULONGLONG n, LPCTSTR lpszSection)
{if (lpszSection != NULL)m_strSection = lpszSection;TCHAR szBuffer[MAX_PATH];_ui64tot(n, szBuffer, 10);WritePrivateProfileString(m_strSection, lpszEntry, szBuffer, m_strFileName);
}void CIni::WriteWORD(LPCTSTR lpszEntry, WORD n, LPCTSTR lpszSection)
{if (lpszSection != NULL)m_strSection = lpszSection;TCHAR szBuffer[MAX_PATH];_ultot(n, szBuffer, 10);WritePrivateProfileString(m_strSection, lpszEntry, szBuffer, m_strFileName);
}void CIni::WriteBool(LPCTSTR lpszEntry, bool b, LPCTSTR lpszSection)
{if (lpszSection != NULL)m_strSection = lpszSection;TCHAR szBuffer[MAX_PATH];_sntprintf(szBuffer, _countof(szBuffer), _T("%d"), (int)b);szBuffer[_countof(szBuffer) - 1] = _T('\0');WritePrivateProfileString(m_strSection, lpszEntry, szBuffer, m_strFileName);
}void CIni::WritePoint(LPCTSTR lpszEntry, CPoint pt, LPCTSTR lpszSection)
{if (lpszSection != NULL)m_strSection = lpszSection;CString strBuffer;strBuffer.Format(_T("(%d,%d)"), pt.x, pt.y);Write(m_strFileName, m_strSection, lpszEntry, strBuffer);
}void CIni::WriteRect(LPCTSTR lpszEntry, CRect rect, LPCTSTR lpszSection)
{if (lpszSection != NULL)m_strSection = lpszSection;CString strBuffer;strBuffer.Format(_T("(%d,%d,%d,%d)"), rect.top, rect.left, rect.bottom, rect.right);Write(m_strFileName, m_strSection, lpszEntry, strBuffer);
}void CIni::WriteColRef(LPCTSTR lpszEntry, COLORREF cr, LPCTSTR lpszSection)
{if (lpszSection != NULL)m_strSection = lpszSection;CString strBuffer;strBuffer.Format(_T("RGB(%d,%d,%d)"), GetRValue(cr), GetGValue(cr), GetBValue(cr));Write(m_strFileName, m_strSection, lpszEntry, strBuffer);
}TCHAR* CIni::GetLPCSTR(LPCTSTR lpszEntry, LPCTSTR lpszSection, LPCTSTR lpszDefault)
{if (lpszSection != NULL)m_strSection = lpszSection;CString strTemp;if (lpszDefault == NULL)strTemp = Read(m_strFileName, m_strSection, lpszEntry, CString());elsestrTemp = Read(m_strFileName, m_strSection, lpszEntry, lpszDefault);return (TCHAR *)memcpy(m_chBuffer, (LPCTSTR)strTemp, (strTemp.GetLength() + 1)*sizeof(TCHAR));
}void CIni::SerGetString(bool bGet, CString &rstr, LPCTSTR lpszEntry, LPCTSTR lpszSection, LPCTSTR lpszDefault)
{if (bGet)rstr = GetString(lpszEntry, lpszDefault, lpszSection);elseWriteString(lpszEntry, rstr, lpszSection);
}void CIni::SerGetDouble(bool bGet, double &f, LPCTSTR lpszEntry, LPCTSTR lpszSection, double fDefault)
{if (bGet)f = GetDouble(lpszEntry, fDefault, lpszSection);elseWriteDouble(lpszEntry, f, lpszSection);
}void CIni::SerGetFloat(bool bGet, float &f, LPCTSTR lpszEntry, LPCTSTR lpszSection, float fDefault)
{if (bGet)f = GetFloat(lpszEntry, fDefault, lpszSection);elseWriteFloat(lpszEntry, f, lpszSection);
}void CIni::SerGetInt(bool bGet, int &n, LPCTSTR lpszEntry, LPCTSTR lpszSection, int nDefault)
{if (bGet)n = GetInt(lpszEntry, nDefault, lpszSection);elseWriteInt(lpszEntry, n, lpszSection);
}void CIni::SerGetDWORD(bool bGet, DWORD &n, LPCTSTR lpszEntry, LPCTSTR lpszSection, DWORD nDefault)
{if (bGet)n = (DWORD)GetInt(lpszEntry, nDefault, lpszSection);elseWriteInt(lpszEntry, n, lpszSection);
}void CIni::SerGetBool(bool bGet, bool &b, LPCTSTR lpszEntry, LPCTSTR lpszSection, bool bDefault)
{if (bGet)b = GetBool(lpszEntry, bDefault, lpszSection);elseWriteBool(lpszEntry, b, lpszSection);
}void CIni::SerGetPoint(bool bGet, CPoint &pt, LPCTSTR lpszEntry, LPCTSTR lpszSection, CPoint ptDefault)
{if (bGet)pt = GetPoint(lpszEntry, ptDefault, lpszSection);elseWritePoint(lpszEntry, pt, lpszSection);
}void CIni::SerGetRect(bool bGet, CRect & rect, LPCTSTR lpszEntry, LPCTSTR lpszSection, CRect rectDefault)
{if (bGet)rect = GetRect(lpszEntry, rectDefault, lpszSection);elseWriteRect(lpszEntry, rect, lpszSection);
}void CIni::SerGetColRef(bool bGet, COLORREF &cr, LPCTSTR lpszEntry, LPCTSTR lpszSection, COLORREF crDefault)
{if (bGet)cr = GetColRef(lpszEntry, crDefault, lpszSection);elseWriteColRef(lpszEntry, cr, lpszSection);
}void CIni::SerGet(bool bGet, CString &rstr, LPCTSTR lpszEntry, LPCTSTR lpszSection, LPCTSTR lpszDefault)
{SerGetString(bGet, rstr, lpszEntry, lpszSection, lpszDefault);
}void CIni::SerGet(bool bGet, double &f, LPCTSTR lpszEntry, LPCTSTR lpszSection, double fDefault)
{SerGetDouble(bGet, f, lpszEntry, lpszSection, fDefault);
}void CIni::SerGet(bool bGet, float &f, LPCTSTR lpszEntry, LPCTSTR lpszSection, float fDefault)
{SerGetFloat(bGet, f, lpszEntry, lpszSection, fDefault);
}void CIni::SerGet(bool bGet, int &n, LPCTSTR lpszEntry, LPCTSTR lpszSection, int nDefault)
{SerGetInt(bGet, n, lpszEntry, lpszSection, nDefault);
}void CIni::SerGet(bool bGet, short &n, LPCTSTR lpszEntry, LPCTSTR lpszSection, int nDefault)
{int nTemp = n;SerGetInt(bGet, nTemp, lpszEntry, lpszSection, nDefault);n = (short)nTemp;
}void CIni::SerGet(bool bGet, DWORD &n, LPCTSTR lpszEntry, LPCTSTR lpszSection, DWORD nDefault)
{SerGetDWORD(bGet, n, lpszEntry, lpszSection, nDefault);
}void CIni::SerGet(bool bGet, WORD &n, LPCTSTR lpszEntry, LPCTSTR lpszSection, DWORD nDefault)
{DWORD dwTemp = n;SerGetDWORD(bGet, dwTemp, lpszEntry, lpszSection, nDefault);n = (WORD)dwTemp;
}void CIni::SerGet(bool bGet, CPoint &pt, LPCTSTR lpszEntry, LPCTSTR lpszSection, CPoint ptDefault)
{SerGetPoint(bGet, pt, lpszEntry, lpszSection, ptDefault);
}void CIni::SerGet(bool bGet, CRect &rect, LPCTSTR lpszEntry, LPCTSTR lpszSection, CRect rectDefault)
{SerGetRect(bGet, rect, lpszEntry, lpszSection, rectDefault);
}void CIni::SerGet(bool bGet, CString *ar, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection, LPCTSTR lpszDefault)
{if (nCount > 0) {CString strBuffer;if (bGet) {strBuffer = GetString(lpszEntry, _T(""), lpszSection);int nOffset = 0;for (int i = 0; i }void CIni::SerGet(bool bGet, double *ar, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection, double fDefault)
{if (nCount > 0) {CString strBuffer;if (bGet) {strBuffer = GetString(lpszEntry, _T(""), lpszSection);CString strTemp;int nOffset = 0;for (int i = 0; i }void CIni::SerGet(bool bGet, float *ar, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection, float fDefault)
{if (nCount > 0) {CString strBuffer;if (bGet) {strBuffer = GetString(lpszEntry, _T(""), lpszSection);CString strTemp;int nOffset = 0;for (int i = 0; i }void CIni::SerGet(bool bGet, int *ar, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection, int iDefault)
{if (nCount > 0) {CString strBuffer;if (bGet) {strBuffer = GetString(lpszEntry, _T(""), lpszSection);CString strTemp;int nOffset = 0;for (int i = 0; i }void CIni::SerGet(bool bGet, unsigned char *ar, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection, unsigned char ucDefault)
{if (nCount > 0) {CString strBuffer;if (bGet) {strBuffer = GetString(lpszEntry, _T(""), lpszSection);CString strTemp;int nOffset = 0;for (int i = 0; i }void CIni::SerGet(bool bGet, short *ar, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection, int iDefault)
{if (nCount > 0) {CString strBuffer;if (bGet) {strBuffer = GetString(lpszEntry, _T(""), lpszSection);CString strTemp;int nOffset = 0;for (int i = 0; i }void CIni::SerGet(bool bGet, DWORD *ar, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection, DWORD dwDefault)
{if (nCount > 0) {CString strBuffer;if (bGet) {strBuffer = GetString(lpszEntry, _T(""), lpszSection);CString strTemp;int nOffset = 0;for (int i = 0; i }void CIni::SerGet(bool bGet, WORD *ar, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection, DWORD dwDefault)
{if (nCount > 0) {CString strBuffer;if (bGet) {strBuffer = GetString(lpszEntry, _T(""), lpszSection);CString strTemp;int nOffset = 0;for (int i = 0; i }void CIni::SerGet(bool bGet, CPoint * ar, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection, CPoint ptDefault)
{CString strBuffer;for (int i = 0; i }void CIni::SerGet(bool bGet, CRect *ar, int nCount, LPCTSTR lpszEntry, LPCTSTR lpszSection, CRect rcDefault)
{CString strBuffer;for (int i = 0; i }int CIni::Parse(const CString &strIn, int nOffset, CString &strOut)
{strOut.Empty();int nLength = strIn.GetLength();if (nOffset }CString CIni::Read(LPCTSTR lpszFileName, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault)
{CString strReturn;GetPrivateProfileString(lpszSection,lpszEntry,lpszDefault,strReturn.GetBufferSetLength(MAX_INI_BUFFER),MAX_INI_BUFFER,lpszFileName);strReturn.ReleaseBuffer();return strReturn;
}void CIni::Write(LPCTSTR lpszFileName, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue)
{WritePrivateProfileString(lpszSection,lpszEntry,lpszValue,lpszFileName);
}bool CIni::GetBinary(LPCTSTR lpszEntry, BYTE** ppData, UINT* pBytes, LPCTSTR pszSection)
{*ppData = NULL;*pBytes = 0;CString str = GetString(lpszEntry, NULL, pszSection);if (str.IsEmpty())return false;ASSERT(str.GetLength()%2 == 0);INT_PTR nLen = str.GetLength();*pBytes = UINT(nLen)/2;*ppData = new BYTE[*pBytes];for (int i=0;i}bool CIni::WriteBinary(LPCTSTR lpszEntry, LPBYTE pData, UINT nBytes, LPCTSTR pszSection)
{// convert to string and write outLPTSTR lpsz = new TCHAR[nBytes*2+1];UINT i;for (i = 0; i > 4) & 0x0F) + 'A'); //high nibble}lpsz[i*2] = 0;WriteString(lpszEntry, lpsz, pszSection);delete[] lpsz;return true;
}void CIni::DeleteKey(LPCTSTR pszKey)
{WritePrivateProfileString(m_strSection, pszKey, NULL, m_strFileName);
}

 

 

 

 

还有一个字符串转换的类库StringConversion.h:

#pragma once
#include
#include int utf8towc(LPCSTR pcUtf8, UINT uUtf8Size, LPWSTR pwc, UINT uWideCharSize)
{LPWSTR pwc0 &#61; pwc;while (uUtf8Size && uWideCharSize){BYTE ucChar &#61; *pcUtf8&#43;&#43;;if (ucChar <0x80){uUtf8Size--;uWideCharSize--;*(pwc&#43;&#43;) &#61; ucChar;}else if ((ucChar & 0xC0) !&#61; 0xC0){return -1; // Invalid UTF8 string..}else{BYTE ucMask &#61; 0xE0;UINT uExpectedBytes &#61; 1;while ((ucChar & ucMask) &#61;&#61; ucMask){ucMask |&#61; ucMask >> 1;if (&#43;&#43;uExpectedBytes > 3)return -1; // Invalid UTF8 string..}if (uUtf8Size <&#61; uExpectedBytes)return -1; // Invalid UTF8 string..UINT uProcessedBytes &#61; 1 &#43; uExpectedBytes;UINT uWideChar &#61; (UINT)(ucChar & ~ucMask);if (uExpectedBytes &#61;&#61; 1){if ((uWideChar & 0x1E) &#61;&#61; 0)return -1; // Invalid UTF8 string..}else{if (uWideChar &#61;&#61; 0 && ((BYTE)*pcUtf8 & 0x3F & (ucMask <<1)) &#61;&#61; 0)return -1; // Invalid UTF8 string..if (uExpectedBytes &#61;&#61; 2){//if (uWideChar &#61;&#61; 0x0D && ((BYTE)*pcUtf8 & 0x20))// return -1;}else if (uExpectedBytes &#61;&#61; 3){if (uWideChar > 4)return -1; // Invalid UTF8 string..if (uWideChar &#61;&#61; 4 && ((BYTE)*pcUtf8 & 0x30))return -1; // Invalid UTF8 string..}}if (uWideCharSize <(UINT)(uExpectedBytes > 2) &#43; 1)break; // buffer fullwhile (uExpectedBytes--){if (((ucChar &#61; (BYTE)*(pcUtf8&#43;&#43;)) & 0xC0) !&#61; 0x80)return -1; // Invalid UTF8 string..uWideChar <<&#61; 6;uWideChar |&#61; (ucChar & 0x3F);}uUtf8Size -&#61; uProcessedBytes;if (uWideChar <0x10000){uWideCharSize--;*(pwc&#43;&#43;) &#61; (WCHAR)uWideChar;}else {uWideCharSize -&#61; 2;uWideChar -&#61; 0x10000;*(pwc&#43;&#43;) &#61; (WCHAR)(0xD800 | (uWideChar >> 10));*(pwc&#43;&#43;) &#61; (WCHAR)(0xDC00 | (uWideChar & 0x03FF));}}}return (int)(pwc - pwc0);
}CString OptUtf8ToStr(const CStringA& rastr)
{CStringW wstr;int iMaxWideStrLen &#61; rastr.GetLength();LPWSTR pwsz &#61; wstr.GetBuffer(iMaxWideStrLen);int iWideChars &#61; utf8towc(rastr, rastr.GetLength(), pwsz, iMaxWideStrLen);if (iWideChars <&#61; 0){// invalid UTF8 string...wstr.ReleaseBuffer(0);wstr &#61; rastr; // convert with local codepage}elsewstr.ReleaseBuffer(iWideChars);return wstr; // just return the string
}CStringA wc2utf8(const CStringW& rwstr)
{CStringA strUTF8;int iChars &#61; AtlUnicodeToUTF8(rwstr, rwstr.GetLength(), NULL, 0);if (iChars > 0){LPSTR pszUTF8 &#61; strUTF8.GetBuffer(iChars);AtlUnicodeToUTF8(rwstr, rwstr.GetLength(), pszUTF8, iChars);strUTF8.ReleaseBuffer(iChars);}return strUTF8;
}CStringA StrToUtf8(const CString& rstr)
{return wc2utf8(rstr);
}

 

 

 

 

下面是测试demo&#xff0c;使用的是mfc&#xff0c;vs2008,贴出关键代码&#xff1a;

void CCIniTest2Dlg::OnBnClickedOk()//读取ini文件
{// TODO: 在此添加控件通知处理程序代码CIni out;LPBYTE msg;UINT msgLen;out.GetBinary(_T("entry-1"),&msg,&msgLen,_T("root section"));msg[msgLen]&#61;NULL;::MessageBoxA(NULL,(char*)msg,"section-1 in entry-1",0);
}void CCIniTest2Dlg::OnBnClickedButton1()//写入ini文件
{// TODO: 在此添加控件通知处理程序代码CIni in;char msg[255];sprintf(msg,"百度and谷歌");in.WriteBinary(_T("entry-1"),(LPBYTE)msg,(UINT)strlen(msg),_T("root section"));in.WriteInt(_T("entry-2"),123456,_T("root section"));
}

 

完成工程下载&#xff1a;http://download.csdn.net/detail/liyoubaidu/4693142

ps:能给点评论&#xff0c;交流一下呢&#xff1f;


转载于:https://www.cnblogs.com/liyou-blog/archive/2012/10/27/read_and_write_iniFile_with_multi_data_type.html


推荐阅读
  • NOIP2000的单词接龙问题与常见的成语接龙游戏有异曲同工之妙。题目要求在给定的一组单词中,从指定的起始字母开始,构建最长的“单词链”。每个单词在链中最多可出现两次。本文将详细解析该题目的解法,并分享学习过程中的心得体会。 ... [详细]
  • 本指南从零开始介绍Scala编程语言的基础知识,重点讲解了Scala解释器REPL(读取-求值-打印-循环)的使用方法。REPL是Scala开发中的重要工具,能够帮助初学者快速理解和实践Scala的基本语法和特性。通过详细的示例和练习,读者将能够熟练掌握Scala的基础概念和编程技巧。 ... [详细]
  • Objective-C 中的委托模式详解与应用 ... [详细]
  • 单链表的高效遍历及性能优化策略
    本文探讨了单链表的高效遍历方法及其性能优化策略。在单链表的数据结构中,插入操作的时间复杂度为O(n),而遍历操作的时间复杂度为O(n^2)。通过在 `LinkList.h` 和 `main.cpp` 文件中对单链表进行封装,我们实现了创建和销毁功能的优化,提高了单链表的使用效率。此外,文章还介绍了几种常见的优化技术,如缓存节点指针和批量处理,以进一步提升遍历性能。 ... [详细]
  • 本文详细介绍了使用 Python 进行 MySQL 和 Redis 数据库操作的实战技巧。首先,针对 MySQL 数据库,通过 `pymysql` 模块展示了如何连接和操作数据库,包括建立连接、执行查询和更新等常见操作。接着,文章深入探讨了 Redis 的基本命令和高级功能,如键值存储、列表操作和事务处理。此外,还提供了多个实际案例,帮助读者更好地理解和应用这些技术。 ... [详细]
  • 在当前的软件开发领域,Lua 作为一种轻量级脚本语言,在 .NET 生态系统中的应用逐渐受到关注。本文探讨了 Lua 在 .NET 环境下的集成方法及其面临的挑战,包括性能优化、互操作性和生态支持等方面。尽管存在一定的技术障碍,但通过不断的学习和实践,开发者能够克服这些困难,拓展 Lua 在 .NET 中的应用场景。 ... [详细]
  • 在Django中提交表单时遇到值错误问题如何解决?
    在Django项目中,当用户提交包含多个选择目标的表单时,可能会遇到值错误问题。本文将探讨如何通过优化表单处理逻辑和验证机制来有效解决这一问题,确保表单数据的准确性和完整性。 ... [详细]
  • 深入解析C#中app.config文件的配置与修改方法
    在C#开发过程中,经常需要对系统的配置文件进行读写操作,如系统初始化参数的修改或运行时参数的更新。本文将详细介绍如何在C#中正确配置和修改app.config文件,包括其结构、常见用法以及最佳实践。此外,还将探讨exe.config文件的生成机制及其在不同环境下的应用,帮助开发者更好地管理和维护应用程序的配置信息。 ... [详细]
  • Presto:高效即席查询引擎的深度解析与应用
    本文深入解析了Presto这一高效的即席查询引擎,详细探讨了其架构设计及其优缺点。Presto通过内存到内存的数据处理方式,显著提升了查询性能,相比传统的MapReduce查询,不仅减少了数据传输的延迟,还提高了查询的准确性和效率。然而,Presto在大规模数据处理和容错机制方面仍存在一定的局限性。本文还介绍了Presto在实际应用中的多种场景,展示了其在大数据分析领域的强大潜力。 ... [详细]
  • 数据库多表联合查询:内连接与外连接详解
    在数据库的多表查询中,内连接和外连接是两种常用的技术手段。内连接用于检索多个表中相互匹配的记录,即只有当两个表中的记录满足特定的连接条件时,这些记录才会被包含在查询结果中。相比之下,外连接则不仅返回匹配的记录,还可以选择性地返回不匹配的记录,具体取决于左外连接、右外连接或全外连接的选择。本文将详细解析这两种连接方式的使用场景及其语法结构,帮助读者更好地理解和应用多表查询技术。 ... [详细]
  • 在洛谷 P1344 的坏牛奶追踪问题中,第一问要求计算最小割,而第二问则需要找到割边数量最少的最小割。通过为每条边附加一个单位权值,可以在求解最小割时优先选择边数较少的方案,从而同时解决两个问题。这种策略不仅简化了问题的求解过程,还确保了结果的最优性。 ... [详细]
  • 第六章:枚举类型与switch结构的应用分析
    第六章深入探讨了枚举类型与 `switch` 结构在编程中的应用。枚举类型(`enum`)是一种将一组相关常量组织在一起的数据类型,广泛存在于多种编程语言中。例如,在 Cocoa 框架中,处理文本对齐时常用 `NSTextAlignment` 枚举来表示不同的对齐方式。通过结合 `switch` 结构,可以更清晰、高效地实现基于枚举值的逻辑分支,提高代码的可读性和维护性。 ... [详细]
  • Android中将独立SO库封装进JAR包并实现SO库的加载与调用
    在Android开发中,将独立的SO库封装进JAR包并实现其加载与调用是一个常见的需求。本文详细介绍了如何将SO库嵌入到JAR包中,并确保在外部应用调用该JAR包时能够正确加载和使用这些SO库。通过这种方式,开发者可以更方便地管理和分发包含原生代码的库文件,提高开发效率和代码复用性。文章还探讨了常见的问题及其解决方案,帮助开发者避免在实际应用中遇到的坑。 ... [详细]
  • 具备括号和分数功能的高级四则运算计算器
    本研究基于C语言开发了一款支持括号和分数运算的高级四则运算计算器。该计算器通过模拟手算过程,对每个运算符进行优先级标记,并按优先级从高到低依次执行计算。其中,加减运算的优先级最低,为0。此外,该计算器还支持复杂的分数运算,能够处理包含括号的表达式,提高了计算的准确性和灵活性。 ... [详细]
  • 在使用SSH框架进行项目开发时,经常会遇到一些常见的问题。例如,在Spring配置文件中配置AOP事务声明后,进行单元测试时可能会出现“No Hibernate Session bound to thread”的错误。本文将详细探讨这一问题的原因,并提供有效的解决方案,帮助开发者顺利解决此类问题。 ... [详细]
author-avatar
哲玲旭辉9
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有