eVC实现INI格式文件操作(Unicode)

本文介绍了如何在eVC中实现对INI配置文件的创建、添加、修改和读取操作,由于eVC缺少内置的API,作者提供了静态函数实现,包括WriteProfileString、GetProfileString、WriteProfileInt和GetProfileInt。文章附带了具体实现代码和实例下载链接,但指出该实现存在缺陷,如在读取时未找到指定键值会继续查找,且键名、等于号和键值之间不允许有空格。请注意,此实现仅支持Unicode格式的INI文件,不支持ANSI格式的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 INI文件作为配置文件的一种相信大家都不陌生,但在eVC中并没有我们所熟悉的GetPrivateProfileString,WritePrivateProfileString等可以方便用来操作INI文件的API函数,因此就需要我们自己去实现;

函数定义:

  1. static BOOL WriteProfileString(const CString strSection, const CString strEntry,
  2.         const CString strValue, const CString strIniPath);
  3. static CString GetProfileString(const CString strSection, const CString strEntry,
  4.         const CString strDefault, const CString strIniPath);
  5. static BOOL WriteProfileInt(const CString strSection, const CString strEntry, 
  6.         const int iValue, const CString strIniPath);
  7. static int GetProfileInt(const CString strSection, const CString strEntry,
  8.         const int iDefault, const CString strIniPath);

具体实现代码和实例下载:http://download.youkuaiyun.com/source/834404(不能下载的可以留下Email)

功能:实现Unicode型的INI格式文件的创建、添加、修改和读取
缺陷:(待改进)
   1、在读取某个项下的某个KEY时,若未找到仍会继续找后面项中的相同的KEY值
   2、在键名、等于号、键值三者之间不能留空格
 注意:INI格式的文件必须是Unicode格式,不支持ANSI格式文件需先转换成Unicode编码

  1. 应用实例(BY Favory.Peng)
  2. 写INI格式文件
  3. CProfile::WriteProfileString(_T("info"),_T("version"),_T("V1.00"),_T("//My Documents//test.ini"));
  4. CProfile::WriteProfileString(_T("config"),_T("name"),_T("INI读写程序"),_T("//My Documents//test.ini"));
  5. CProfile::WriteProfileInt(_T("config"),_T("value"),200812,_T("//My Documents//test.ini"));
  6. 读INI格式文件
  7. CString textval=CProfile::GetProfileString(_T("info"),_T("version"),_T("error"),_T("//My Documents//test.ini"));
  8. AfxMessageBox(textval); 
  9. textval=CProfile::GetProfileString(_T("config"),_T("name"),_T("error"),_T("//My Documents//test.ini"));
  10. AfxMessageBox(textval); 
  11. INT val=CProfile::GetProfileInt(_T("config"),_T("value"),0,_T("//My Documents//test.ini"));
  12. textval.Format(_T("get value=%d"),val);
  13. AfxMessageBox(textval);

注:CProfile的原始代码来自网络,它是借助CSting类来实现,方法简单明了,但功能还存在一些缺陷,有待进一步改进,需要在使用过程中加以注意。最后感谢原始作者提供代码与大家分享,希望大家共同优化完善它。

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值