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

來源:互聯網
上載者:User

    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 {
showTip(this)
}">lpAppName
,   // section name
  LPCTSTR {
showTip(this)
}">lpKeyName
,   // key name
  LPCTSTR {
showTip(this)
}">lpString
,         // string to add
  LPCTSTR {
showTip(this)
}">lpFileName
     // initialization file
);

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

 

UINT GetPrivateProfileInt(
  LPCTSTR {
showTip(this)
}">lpAppName
,  // section name
  LPCTSTR {
showTip(this)
}">lpKeyName
,  // key name
  INT {
showTip(this)
}">nDefault
,       // return value if key name not found
  LPCTSTR {
showTip(this)
}">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();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.