WinCE读写ini文件和xml文件的方法
为了解决代码的可以执行和可重用性,我们一般会利用一些配置来达到我们的目的。目前流行的配置文件有用INI文件的,也有很多利用XML文件的,或者写数据库,写注册表的。像.Net平台可以直接添加应用程序的配置文件(如app.config),同时它还提供了相关的类库来操作这种特定类型的配置文件,在Win32环境下也提供了诸如GetPrivateProfileInt(),GetPrivateProfileString()等API来实现读取ini文件。但是在PPC环境中,不管是读取XML,还是ini文件,都没有提供相关的库来供我们调用,这样就需要我们自己实现一个类似的方法来读取我们所需的XML或者ini类似的配置文件了,不过幸运的是,很多慷慨的同志们已经将他们的代码公布于网上了,如果要理解利用EVC或者VC操作XML文件,请查看 http://hi.baidu.com/yxifu/blog/item/fa1569225bda52a44623e8f0.html
本文主要讲解利用EVC读写ini文件。 u ini 文件格式 ini 文件是文本文件,中间的数据格式一般为: [Section1 Name] KeyName1=value1 KeyName2=value2 ...
[Section2 Name] KeyName1=value1 KeyName2=value2
文件可以分为几个 Section,每个 Section 的名称用 [] 括起来,在一个 Section 中,可以有很多的 Key,每一个 Key 可以有一个值并占用一行,格式是 Key=value u 读写原理 注:本文提供的代码也是从网上搜索而来,在自己调试后,发现读写都有点小问题,修改了下,同时这里也约定,每个Key和value之间不能有空格,不然程序会出错。 其中注释的地方为我修改过的地方 文件示例: Interval=5000
代码示例: 1.写内容
u Demo下载 BOOL CProfile::WriteProfileString( const CString strSection, const CString strEntry, const CString strValue, const CString strIniPath) { if (strSection == L "" || strEntry == L "" || strValue == L "" || strIniPath == L "" ) { return FALSE; } CFile IniFile; CString strCombine; TRY { if ( ! IniFile.Open(strIniPath,CFile::modeReadWrite | CFile::modeCreate | CFile::modeNoTruncate)) { return FALSE; } if (IniFile.GetLength() == 0 ) { strCombine = L " [ " + strSection + L " ] " + L " rn " + strEntry + L " = " + strValue + L " rn " ; LPTSTR lpCombine = strCombine.GetBuffer( 0 ); IniFile.Write(lpCombine,strCombine.GetLength() * 2 ); IniFile.Close(); return TRUE; } WCHAR * pBuf; pBuf = new WCHAR[IniFile.GetLength() / 2 + 1 ]; if (pBuf == NULL) { IniFile.Close(); return FALSE; } if (IniFile.Read(pBuf,IniFile.GetLength()) != IniFile.GetLength()) { delete[] pBuf; IniFile.Close(); return FALSE; } pBuf[IniFile.GetLength() / 2 ] = NULL; strCombine.GetBuffer(MAX_LEN); strCombine = pBuf; delete[] pBuf; int iIndex1,iIndex2,iIndex3,iIndexT; iIndex1 = strCombine.Find(L " [ " + strSection + L " ]rn " ); if (iIndex1 == - 1 ) { strCombine += L " [ " + strSection + L " ] " + L " rn " + strEntry + L " = " + strValue + L " rn " ; LPTSTR lpCombine = strCombine.GetBuffer( 0 ); IniFile.SetLength( 0 ); IniFile.SeekToBegin(); IniFile.Write(lpCombine,strCombine.GetLength() * 2 ); IniFile.Close(); return TRUE; } // iIndexT = iIndex1 + 4 + strSection.GetLength(); // modify by plr at 2008-11-8 iIndexT = iIndex1 + strSection.GetLength() + 2 ; // 2代表"[]"两个字符 iIndex2 = strCombine.Find(strEntry + L " = " ,iIndexT); if (iIndex2 == - 1 ) { strCombine.Insert(iIndexT,strEntry + L " = " + strValue + L " rn " ); LPTSTR lpCombine = strCombine.GetBuffer( 0 ); IniFile.SetLength( 0 ); IniFile.SeekToBegin(); IniFile.Write(lpCombine,strCombine.GetLength() * 2 ); IniFile.Close(); return TRUE; } else { iIndex3 = strCombine.Find(L " rn " ,iIndex2 + 1 ); if (iIndex3 == - 1 ) { IniFile.Close(); return FALSE; } iIndexT = iIndex2 + 1 + strEntry.GetLength(); strCombine.Delete(iIndexT,iIndex3 - iIndexT); strCombine.Insert(iIndexT,strValue); LPTSTR lpCombine = strCombine.GetBuffer( 0 ); IniFile.SetLength( 0 ); IniFile.SeekToBegin(); IniFile.Write(lpCombine,strCombine.GetLength() * 2 ); IniFile.Close(); return TRUE; } } CATCH(CFileException,e) { } END_CATCH IniFile.Close(); return FALSE; } BOOL CProfile::WriteProfileInt(const CString strSection,const CString strEntry,const int iValue,const CString strIniPath) { wchar_t cBuff[MAX_LEN]; CString strValue(""); _itow(iValue,cBuff,10); strValue.Format(_T("%s"),cBuff); return CProfile::WriteProfileString(strSection,strEntry,strValue,strIniPath); } 2.读配置文件 CString CProfile::GetProfileString(const CString strSection,const CString strDefault,const CString strIniPath) { if(strSection == L"" || strEntry == L"" || strIniPath == L"") { return strDefault; } CFile IniFile; CString strCombine; TRY { if(! IniFile.Open(strIniPath,CFile::modeRead)) { return strDefault; } if(IniFile.GetLength() == 0) { IniFile.Close(); return strDefault; } WCHAR *pBuf; pBuf = new WCHAR[IniFile.GetLength() / 2 + 1]; if(pBuf == NULL) { IniFile.Close(); return strDefault; } if(IniFile.Read(pBuf,IniFile.GetLength()) != IniFile.GetLength()) { delete[] pBuf; IniFile.Close(); return strDefault; } pBuf[IniFile.GetLength() / 2] = NULL; strCombine.GetBuffer(MAX_LEN); strCombine = pBuf; delete[] pBuf; int iIndex1,iIndexT; iIndex1 = strCombine.Find(L"[" + strSection + L"]rn"); if(iIndex1 == -1) { IniFile.Close(); return strDefault; } iIndexT = iIndex1 + strSection.GetLength() + 2; //2代表"[]"两个字符 iIndex2 = strCombine.Find(strEntry + L"=",iIndexT); if(iIndex2 == -1) { IniFile.Close(); return strDefault; } else { iIndex3 = strCombine.Find(L"rn",iIndex2 + 1); if(iIndex3 == -1) { IniFile.Close(); return strDefault; } iIndexT = iIndex2 + 1 + strEntry.GetLength(); //这里1代表'='的长度 IniFile.Close(); return strCombine.Mid(iIndexT,iIndex3 - iIndexT); } } CATCH(CFileException,e) { } END_CATCH IniFile.Close(); return strDefault;} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |