eVC實現INI格式文件操作(Unicode)

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

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章