当前位置: 首页 > news >正文

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       读写原理
将文件的整个内容看成是一个字符串,读写配置节和配置内容就相当于对字符串做操作,利用GetBuffer, Find, Mid()方法都配置文件,利用Delete,Insert()方法写文件。

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

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

文件示例
[GPS]

Interval=5000

 

代码示例:

 1.写内容

 

u       Demo下载
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;
}
 
 

 

 

ContractedBlock.gif ExpandedBlockStart.gif 写整型

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.读配置文件

 

ContractedBlock.gif ExpandedBlockStart.gif Code

CString CProfile::GetProfileString(
const CString strSection, const CString strEntry, 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, iIndex2, iIndex3, 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;
}

相关文章:

  • Linux GNOME桌面使用技巧大全
  • 转:WEB页上弹消息框总汇~!
  • 解决AJAX中使用UpdatePanel后再用Response.Write();等无法弹出对话框问题
  • 用于收发电子邮件的应用类
  • 在自己网站加百度搜索框这么简单
  • ubuntu下root 密码忘记的解决方法
  • 从“芯”认识内存
  • Using LINQ in ASP.NET (1)
  • 又开始了Vmware……
  • 安装完流媒体服务器WP9Server,后发现原iis默认80端口被占用:
  • 跟小段一起学Solaris(14)---FTP服务
  • LINUX网络服务 DHCP服务
  • Cisco路由器上如何防止DDoS攻击
  • 红旗6SP2编译安装kernel 2.6.30-rc3
  • 天台之约
  • android图片蒙层
  • Brief introduction of how to 'Call, Apply and Bind'
  • Java IO学习笔记一
  • Java新版本的开发已正式进入轨道,版本号18.3
  • MySQL几个简单SQL的优化
  • python 学习笔记 - Queue Pipes,进程间通讯
  • Sublime text 3 3103 注册码
  • vue中实现单选
  • win10下安装mysql5.7
  • 从0到1:PostCSS 插件开发最佳实践
  • - 概述 - 《设计模式(极简c++版)》
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 设计模式(12)迭代器模式(讲解+应用)
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 数组大概知多少
  • 双管齐下,VMware的容器新战略
  • 我看到的前端
  • 小程序上传图片到七牛云(支持多张上传,预览,删除)
  • 学习使用ExpressJS 4.0中的新Router
  • 云大使推广中的常见热门问题
  • 智能合约开发环境搭建及Hello World合约
  • Spring Batch JSON 支持
  • # include “ “ 和 # include < >两者的区别
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • #define MODIFY_REG(REG, CLEARMASK, SETMASK)
  • #微信小程序:微信小程序常见的配置传值
  • (笔试题)分解质因式
  • (二)JAVA使用POI操作excel
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (附源码)ssm失物招领系统 毕业设计 182317
  • (十一)c52学习之旅-动态数码管
  • (四)Tiki-taka算法(TTA)求解无人机三维路径规划研究(MATLAB)
  • (算法)前K大的和
  • (推荐)叮当——中文语音对话机器人
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • (一)appium-desktop定位元素原理
  • (原創) 博客園正式支援VHDL語法著色功能 (SOC) (VHDL)
  • (转载)OpenStack Hacker养成指南
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .cfg\.dat\.mak(持续补充)