INI文件读写--VC6.0
生活随笔
收集整理的這篇文章主要介紹了
INI文件读写--VC6.0
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
新建一個dialog based MFC Windows Application,命名為:d, 界面為:
為按鈕Read和Write添加單擊事件,并自定義一個函數GetIniFileName()用來取得ini文件的路徑,主要函數代碼如下: // read data from config file void CDDlg::OnBtnRead() // 從INI文件中讀取指定的信息,更新到編輯框中 {char userName[64], password[64];int score_Chinese, score_Mathematics;CString sIniFile = GetIniFileName(); // 取得配置文件全路徑memset(userName, 0, sizeof(userName)); // 填充字符串數組memset(password, 0, sizeof(password));// read values from the specified config fileGetPrivateProfileString(SECTION_USERINFO, "UserName", "", userName, 63, sIniFile); // read a string from the specified config fileGetPrivateProfileString(SECTION_USERINFO, "Password", "", password, 63, sIniFile);score_Chinese = GetPrivateProfileInt(SECTION_SCORE, "Chinese", 0, sIniFile); // read an integer from the specified config filescore_Mathematics = GetPrivateProfileInt(SECTION_SCORE, "Mathematics", 0, sIniFile);// Update values to the controlsCString tempValue;((CEdit*) GetDlgItem(IDC_EDIT_USERNAME))->SetWindowText(userName);((CEdit*) GetDlgItem(IDC_EDIT_PASSWORD))->SetWindowText(password);tempValue.Format("%d", score_Chinese);((CEdit*) GetDlgItem(IDC_EDIT_CHINESE))->SetWindowText(tempValue); // 10:代表十進制tempValue.Format("%d", score_Mathematics);((CEdit*) GetDlgItem(IDC_EDIT_MATHEMATICS))->SetWindowText(tempValue);MessageBox("Read data from config file successfully!", "Information", MB_OK | MB_ICONINFORMATION); // ((CStatic *) GetDlgItem(IDC_TXT_MESSAGE))->SetWindowText("Read data from config file successfully!"); }// store data to config file void CDDlg::OnBtnwrite() {// TODO: Add your control notification handler code herechar userName[64], password[64], sValue[20];int score_Chinese, score_Mathematics;((CEdit *) GetDlgItem(IDC_EDIT_USERNAME))->GetWindowText(userName, 63);((CEdit *) GetDlgItem(IDC_EDIT_PASSWORD))->GetWindowText(password, 63);((CEdit *) GetDlgItem(IDC_EDIT_CHINESE))->GetWindowText(sValue, 19);score_Chinese = atoi(sValue);((CEdit *) GetDlgItem(IDC_EDIT_MATHEMATICS))->GetWindowText(sValue, 19);score_Mathematics = atoi(sValue);// Write data to the config fileCString sIniFile = GetIniFileName();CString tempValue;WritePrivateProfileString(SECTION_USERINFO, "UserName", userName, sIniFile);WritePrivateProfileString(SECTION_USERINFO, "Password", password, sIniFile);tempValue.Format("%d", score_Chinese);WritePrivateProfileString(SECTION_SCORE, "Chinese", tempValue, sIniFile);tempValue.Format("%d", score_Mathematics);WritePrivateProfileString(SECTION_SCORE, "Mathematics", tempValue, sIniFile);MessageBox("Write data to config file successfully!", "Information", MB_OK | MB_ICONEXCLAMATION); // ((CStatic *) GetDlgItem(IDC_TXT_MESSAGE))->SetWindowText("Write data to config file successfully!"); }// get the file path of config file CString CDDlg::GetIniFileName() const {CString Result, sPath;Result = _T("");char sFileName[256];GetModuleFileName(AfxGetInstanceHandle(), sFileName, 255); // 取得當前可執行文件的路徑,保存在sFileName中sPath = sFileName;int index = sPath.ReverseFind('\\'); // 逆向查找指定字符sPath = sPath.Left(index);Result = sPath + "\\Config.ini";return Result; }其中,ini文件中的內容為: [UserInfo]
UserName=tian
Password=123456zhou [Score]
Chinese=55
Mathematics=66
源碼下載:http://d.download.csdn.net/down/2735337/JoeBlackzqq
總結
以上是生活随笔為你收集整理的INI文件读写--VC6.0的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cocos2d-js 热更新具体解释(一
- 下一篇: c++类模板及参数类型的运行时判断