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.csdn.net/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類來實現,方法簡單明瞭,但功能還存在一些缺陷,有待進一步改進,需要在使用過程中加以注意。最後感謝原始作者提供代碼與大家分享,希望大家共同優化完善它。