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

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

    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();
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值