Configure file like format of ini and read data from ini file or write data to ini file

    Sometimes,It's not that always store data in memory, you should store data to disk file,  because you release memory and destroy address of data  that

allocated to data previously when main thread procedure is destroyed.The data will be lost in memeroy.But you will use data that store in memory previously to do something.so what are you gonna do? here,I give you a method to store data to configure file as following description.

   First,Let me have a look format of ini file as soon as possible. 

     [section]

       key = value

     .........

     ; comment

  and then,how to use configure file to store data,please look the following win-32 main functions:

  

 BOOL WritePrivateProfileString(
  LPCTSTR lpAppName,   // section name
  LPCTSTR lpKeyName,   // key name
  LPCTSTR lpString,         // string to add
  LPCTSTR lpFileName     // initialization file
);

 DWORD GetPrivateProfileString(
  LPCTSTR lpAppName,        // section name
  LPCTSTR lpKeyName,        // key name
  LPCTSTR lpDefault,        // default string
  LPTSTR lpReturnedString,  // destination buffer
  DWORD nSize,              // size of destination buffer
  LPCTSTR lpFileName        // initialization file name
);

 

UINT GetPrivateProfileInt(
  LPCTSTR lpAppName,  // section name
  LPCTSTR lpKeyName,  // key name
  INT nDefault,       // return value if key name not found
  LPCTSTR lpFileName  // initialization file name
);

 

My code:

 

void CLoginDemoDlg::OnBnClickedConfirm()
{
 // TODO: Add your control notification handler code here
 m_UserID.GetWindowText(m_UserName);
 ::WritePrivateProfileString(_T("LoginInfo"),_T("UserID"),//將登錄信息寫入配置文件
  m_UserName,ConfigurePath+_T("LoginInfo.ini"));

}

    void CLoginDemoDlg::AddLanguageString()
{
 CString strTemp;
 CString nStr;
 CString SelString[3];
 int nCount = 3;
 SelString[0] = _T("簡體中文");
 SelString[1] = _T("繁體中文");
 SelString[2] = _T("English");
 for (int i=0; i<3;i++)
 {
  strTemp.Format(_T("%d"),i);
  ::WritePrivateProfileString(_T("LanguageInfo"),_T("Language")+strTemp,
  SelString[i],ConfigurePath + _T("LanguageSel.ini"));
 }
 nStr.Format(_T("%d"),nCount);
 ::WritePrivateProfileString(_T("CountInfo"),_T("Count"),nStr,
  ConfigurePath + _T("LanguageSel.ini"));

}
void CLoginDemoDlg::GetLanguageFile()                                           

{
 CString buffer;
 CString nStr;
 int nIndex;
 nIndex = ::GetPrivateProfileInt(_T("CountInfo"),_T("Count"),0,
  ConfigurePath + _T("LanguageSel.ini"));
 for (int i=0; i<nIndex; i++)
 {
  nStr.Format(_T("%d"),i);
  ::GetPrivateProfileString(_T("LanguageInfo"),_T("Language")+nStr,NULL,
   buffer.GetBuffer(1024),1024,ConfigurePath + _T("LanguageSel.ini"));
  m_Language.AddString(buffer);
  buffer.ReleaseBuffer();
 }
 m_Language.SetCurSel(0);

}

void CLoginDemoDlg::GetLoginFromFile()
{
 CString  strTemp;
 int nIndex = 0;
 ::GetPrivateProfileString(_T("LoginInfo"),_T("UserID"),
  NULL,strTemp.GetBuffer(1024),1024,ConfigurePath + _T("LoginInfo.ini"));
 nIndex = m_UserID.AddString(strTemp);
 m_UserID.SetCurSel(nIndex);
 strTemp.ReleaseBuffer();
}

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