WinCE读写ini文件和xml文件的方法

前端之家收集整理的这篇文章主要介绍了WinCE读写ini文件和xml文件的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

为了解决代码的可以执行和可重用性,我们一般会利用一些配置来达到我们的目的。目前流行的配置文件有用@H_403_3@INI文件的,也有很多利用@H_403_3@XML文件的,或者写数据库,写注册表的。像@H_403_3@.Net平台可以直接添加应用程序的配置文件@H_403_3@(@H_403_3@app.config),同时它还提供了相关的类库来操作这种特定类型的配置文件,在@H_403_3@Win32环境下也提供了诸如@H_403_3@GetPrivateProfileInt(),@H_403_3@GetPrivateProfileString()等@H_403_3@API来实现读取@H_403_3@ini文件。但是在@H_403_3@PPC环境中,不管是读取@H_403_3@XML,还是@H_403_3@ini文件,都没有提供相关的库来供我们调用,这样就需要我们自己实现一个类似的方法来读取我们所需的@H_403_3@XML或者@H_403_3@ini类似的配置文件了,不过幸运的是,很多慷慨的同志们已经将他们的代码公布于网上了,如果要理解利用@H_403_3@EVC或者@H_403_3@VC操作@H_403_3@XML文件,请查看

@H_403_3@http://hi.baidu.com/yxifu/blog/item/fa1569225bda52a44623e8f0.html

@H_403_3@

本文主要讲解利用@H_403_3@EVC读写@H_403_3@ini文件

u @H_403_3@ini 文件格式

@H_403_3@ini 文件是文本文件,中间的数据格式一般为:@H_403_3@

@H_403_3@[Section1 Name]

@H_403_3@KeyName1=value1

@H_403_3@KeyName2=value2

@H_403_3@...

@H_403_3@

@H_403_3@[Section2 Name]

@H_403_3@KeyName1=value1

@H_403_3@KeyName2=value2

@H_403_3@

文件可以分为几个@H_403_3@ Section,每个@H_403_3@ Section 名称@H_403_3@ [] 括起来,在一个@H_403_3@ Section 中,可以有很多的 @H_403_3@Key,每一个@H_403_3@ Key 可以有一个值并占用一行,格式是@H_403_3@ Key=value

u 读写原理@H_403_3@
文件的整个内容看成是一个字符串,读写配置节和配置内容就相当于对字符串做操作,利用@H_403_3@GetBuffer, Find,@H_403_3@ Mid()方法配置文件,利用Delete,Insert()方法文件

注:本文提供的代码也是从网上搜索而来,在自己调试后,发现读写都有点小问题,修改了下,同时这里也约定,每个@H_403_3@Key@H_403_3@value之间不能有空格,不然程序会出错。

其中注释的地方为我修改过的地方

文件示例@H_403_3@
[GPS]

@H_403_3@Interval=5000

@H_403_3@

代码示例:

@H_403_3@1.写内容

@H_403_3@@H_403_3@

u @H_403_3@Demo下载@H_403_3@
http://download.csdn.net/source/773349


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 " \r\n "
+ strEntry + L " = " + strValue + L " \r\n " ;
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 " ]\r\n " );
if (iIndex1 == - 1 )
{
strCombine
+= L " [ " + strSection + L " ] " + L " \r\n "
+ strEntry + L " = " + strValue + L " \r\n " ;

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 " \r\n " );

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 " \r\n " ,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"]\r\n");
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"\r\n",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;}

猜你在找的XML相关文章