Windows Moblie 利用EVC读写INI XML配置文件
2010-08-22 12:59:00 来源:WEB开发网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, iIndex2, iIndex3, iIndexT;
iIndex1 = strCombine.Find(L"[" + strSection + L"] ");
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" ", 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;
}
获取整型
int CProfile::GetProfileInt(const CString strSection, const CString strEntry, const int iDefault, const CString strIniPath)
{
wchar_t cBuff[MAX_LEN];
CString strDefault("");
CString strReturn("");
_itow(iDefault, cBuff, 10);
strDefault.Format(_T("%s"), cBuff);
strReturn = CProfile::GetProfileString(strSection, strEntry, strDefault, strIniPath);
return (_ttoi(strReturn));
}
更多精彩
赞助商链接