INI文件作为配置文件的一种相信大家都不陌生,但在eVC中并没有我们所熟悉的GetPrivateProfileString,WritePrivateProfileString等可以方便用来操作INI文件的API函数,因此就需要我们自己去实现;
函数定义:
- static BOOL WriteProfileString(const CString strSection, const CString strEntry,
- const CString strValue, const CString strIniPath);
- static CString GetProfileString(const CString strSection, const CString strEntry,
- const CString strDefault, const CString strIniPath);
- static BOOL WriteProfileInt(const CString strSection, const CString strEntry,
- const int iValue, const CString strIniPath);
- static int GetProfileInt(const CString strSection, const CString strEntry,
- const int iDefault, const CString strIniPath);
具体实现代码和实例下载:http://download.youkuaiyun.com/source/834404(不能下载的可以留下Email)
功能:实现Unicode型的INI格式文件的创建、添加、修改和读取
缺陷:(待改进)
1、在读取某个项下的某个KEY时,若未找到仍会继续找后面项中的相同的KEY值
2、在键名、等于号、键值三者之间不能留空格
注意:INI格式的文件必须是Unicode格式,不支持ANSI格式文件需先转换成Unicode编码
- 应用实例(BY Favory.Peng)
- 写INI格式文件
- CProfile::WriteProfileString(_T("info"),_T("version"),_T("V1.00"),_T("//My Documents//test.ini"));
- CProfile::WriteProfileString(_T("config"),_T("name"),_T("INI读写程序"),_T("//My Documents//test.ini"));
- CProfile::WriteProfileInt(_T("config"),_T("value"),200812,_T("//My Documents//test.ini"));
- 读INI格式文件
- CString textval=CProfile::GetProfileString(_T("info"),_T("version"),_T("error"),_T("//My Documents//test.ini"));
- AfxMessageBox(textval);
- textval=CProfile::GetProfileString(_T("config"),_T("name"),_T("error"),_T("//My Documents//test.ini"));
- AfxMessageBox(textval);
- INT val=CProfile::GetProfileInt(_T("config"),_T("value"),0,_T("//My Documents//test.ini"));
- textval.Format(_T("get value=%d"),val);
- AfxMessageBox(textval);
注:CProfile的原始代码来自网络,它是借助CSting类来实现,方法简单明了,但功能还存在一些缺陷,有待进一步改进,需要在使用过程中加以注意。最后感谢原始作者提供代码与大家分享,希望大家共同优化完善它。