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();
}
本文介绍如何利用Win32 API中的函数来读取和写入配置文件,确保应用程序数据即使在内存释放后也能持久保存。通过具体代码示例展示了如何将登录信息和语言设置等数据存储到INI文件中。
936

被折叠的 条评论
为什么被折叠?



