如果有一个任务,需要连续的读写ini,因为WritePrivateProfileString GetPrivateProfileString 每次都需要读写ini文件,很浪费时间。
经过测量,如果条目较多(>= 700),时间有700MS以上.
写了一个类,用xml模拟ini的存储样式,不用每次都读写文件,文件的保存时机由自己控制,效率高很多.
工程下载 : src_xmlIni.rar
// xmlIni.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "MyXmlIni.h" int _tmain(int argc,_TCHAR* argv[]) { CMyXmlIni ini; BOOL bRc = FALSE; std::wstring strKey1Value = L""; std::wstring strKey2Value = L""; std::wstring strKeyNValue = L""; ini.SetIniPathName(L"d:\\myIni.xml"); ini.WriteToIni(L"1",L"2",L"3"); ini.SaveXml(); ///< 可以自己选择保存文件的时机 bRc = ini.ReadFromIni(strKey1Value,strKey2Value,strKeyNValue); if (bRc) { _tprintf(L"strKey1Value = %s\r\n",strKey1Value.c_str()); _tprintf(L"strKey2Value = %s\r\n",strKey2Value.c_str()); _tprintf(L"strKeyNValue = %s\r\n",strKeyNValue.c_str()); } else { _tprintf(L"read ini Failed\r\n"); } /** xml文件内容 <ini> <MyIniSection> <MyIniKey1>1</MyIniKey1> <MyIniKey2>2</MyIniKey2> <MyIniKey3>3</MyIniKey3> </MyIniSection> </ini> */ /** run result strKey1Value = 1 strKey2Value = 2 strKeyNValue = 3 */ getchar(); return 0; }
/// @file MyXmlIni.h /// @brief 用Xml模拟Ini那种Section/Key的文件读写过程,/// 应对iniApi每次都读写文件引起的性能瓶颈 #ifndef __MY_XML_INI_H__ #define __MY_XML_INI_H__ #include "stdafx.h" #include "XmlIniBase.h" #define INI_SECTION L"MyIniSection" #define INI_KEY_1 L"MyIniKey1" #define INI_KEY_2 L"MyIniKey2" #define INI_KEY_N L"MyIniKey3" class CMyXmlIni : public CXmlIniBase { public: CMyXmlIni(); virtual ~CMyXmlIni(); BOOL ReadFromIni( std::wstring& strKey1Value,std::wstring& strKey2Value,std::wstring& strKey3Value); BOOL WriteToIni( const WCHAR* pcKey1Value,const WCHAR* pcKey2Value,const WCHAR* pcKeyNValue); }; #endif // #ifndef __MY_XML_INI_H__
/// @file XmlIniBase.h /// @brief 用Xml模拟Ini那种Section/Key的文件读写过程,/// 应对iniApi每次都读写文件引起的性能瓶颈 #ifndef __XML_INI_BASE_H__ #define __XML_INI_BASE_H__ #include "stdafx.h" #include "Markup.h" #ifndef SAFE_DELETE #define SAFE_DELETE(p) \ if (NULL != (p)) \ { \ delete (p); \ (p) = NULL; \ } #endif class CXmlIniBase { public: CXmlIniBase(); virtual ~CXmlIniBase(); void SetIniPathName(const WCHAR* pcIniPathName); BOOL SetDataToXml( const WCHAR* pcSection,const WCHAR* pcKey,const WCHAR* pcValue); BOOL GetDataFromXml( const WCHAR* pcSection,std::wstring & strValue); BOOL FreeXml(); BOOL SaveXml(); private: BOOL IntoRoot(); void CXmlIniBase::SetXmlObj(CMarkup * pObj); CMarkup * CXmlIniBase::GetXmlObj(); private: CMarkup * m_pXml; std::wstring m_strXmlFilePathName; }; #endif // #ifndef __XML_INI_BASE_H__
/// @file XmlIniBase.cpp /// @brief #include "stdafx.h" #include "MyXmlIni.h" CMyXmlIni::CMyXmlIni() { } CMyXmlIni::~CMyXmlIni() { } BOOL CMyXmlIni::ReadFromIni( std::wstring& strKey1Value,std::wstring& strKey3Value) { BOOL bRc = FALSE; int iTemp = 0; std::wstring strTemp = L""; bRc = GetDataFromXml(INI_SECTION,INI_KEY_1,strKey1Value); if (!bRc) return bRc; bRc = GetDataFromXml(INI_SECTION,INI_KEY_2,strKey2Value); if (!bRc) return bRc; bRc = GetDataFromXml(INI_SECTION,INI_KEY_N,strKey3Value); if (!bRc) return bRc; return TRUE; } BOOL CMyXmlIni::WriteToIni( const WCHAR* pcKey1Value,const WCHAR* pcKeyNValue) { BOOL bRc = FALSE; int iTemp = 0; std::wstring strTemp = L""; bRc = SetDataToXml(INI_SECTION,pcKey1Value); _ASSERT(bRc); bRc = SetDataToXml(INI_SECTION,pcKey2Value); _ASSERT(bRc); bRc = SetDataToXml(INI_SECTION,pcKeyNValue); _ASSERT(bRc); return bRc; }
/// @file XmlIniBase.cpp /// @brief #include "stdafx.h" #include "XmlIniBase.h" CXmlIniBase::CXmlIniBase() : m_pXml(NULL),m_strXmlFilePathName(L"") { } CXmlIniBase::~CXmlIniBase() { SAFE_DELETE(m_pXml); } void CXmlIniBase::SetXmlObj(CMarkup* pObj) { m_pXml = pObj; } CMarkup* CXmlIniBase::GetXmlObj() { return m_pXml; } BOOL CXmlIniBase::SaveXml() { bool bRc = false; _ASSERT(!m_strXmlFilePathName.empty()); bRc = m_pXml->Save(m_strXmlFilePathName.c_str()); _ASSERT(true == bRc); return (true == bRc); } BOOL CXmlIniBase::IntoRoot() { BOOL bRc = FALSE; const WCHAR * const pcXmlRootName = L"ini"; CMarkup* pXml = GetXmlObj(); _ASSERT(NULL != pXml); pXml->ResetPos(); ///< ! bRc = pXml->FindElem(pcXmlRootName); ///< xml根节点名称 = ini if (!bRc) { // create one bRc = pXml->AddElem(pcXmlRootName); _ASSERT(bRc); } _ASSERT(bRc); bRc = pXml->IntoElem(); _ASSERT(bRc); return bRc; } void CXmlIniBase::SetIniPathName(const WCHAR* pcIniPathName) { _ASSERT(NULL != pcIniPathName); CMarkup* pXml = NULL; m_strXmlFilePathName = pcIniPathName; pXml = GetXmlObj(); if (NULL != pXml) { FreeXml(); } pXml = new CMarkup; _ASSERT(NULL != pXml); SetXmlObj(pXml); if (!GetXmlObj()->Load(pcIniPathName)) { SaveXml(); } } BOOL CXmlIniBase::SetDataToXml( const WCHAR* pcSection,const WCHAR* pcValue) { BOOL bRc = false; CMarkup* pXml = GetXmlObj(); _ASSERT(NULL != pXml); _ASSERT(NULL != pcSection); _ASSERT(NULL != pcKey); _ASSERT(NULL != pcValue); bRc = IntoRoot(); _ASSERT(bRc); bRc = pXml->FindElem(pcSection); if (!bRc) { // create one bRc = pXml->AddElem(pcSection); _ASSERT(bRc); } pXml->IntoElem(); ///< => pcSection bRc = pXml->FindElem(pcKey); if (!bRc) { // create one bRc = pXml->AddElem(pcKey); _ASSERT(bRc); } if (bRc) { bRc = pXml->SetData((NULL != pcValue) ? pcValue : L""); _ASSERT(bRc); } pXml->OutOfElem(); ///< <= pcSection return bRc; } BOOL CXmlIniBase::GetDataFromXml( const WCHAR* pcSection,std::wstring & strValue) { BOOL bRc = false; CMarkup* pXml = GetXmlObj(); _ASSERT(NULL != pXml); if (NULL == pXml) goto END_GetDataFromXml; _ASSERT(NULL != pcSection); _ASSERT(NULL != pcKey); bRc = IntoRoot(); _ASSERT(bRc); bRc = pXml->FindElem(pcSection); if (!bRc) { goto END_GetDataFromXml; } pXml->IntoElem(); ///< => pcSection bRc = pXml->FindElem(pcKey); if (!bRc) { goto END_GetDataFromXml; } strValue = pXml->GetData(); pXml->OutOfElem(); ///< <= pcSection END_GetDataFromXml: return bRc; } BOOL CXmlIniBase::FreeXml() { SAFE_DELETE(m_pXml); return TRUE; }原文链接:https://www.f2er.com/xml/298121.html