分类: C/C++
2016-07-01 16:03:43
/*
功能:读取和写入ini文件
环境:VS2013 VC++
时间:2016-06-28
作者:Linkun
*///获取路径,生成ini文件名
CString strPath, strFileName;
strFileName = "test.ini";
TCHAR _szPath[MAX_PATH+1] ={0};
GetModuleFileName(NULL, _szPath, MAX_PATH);
(_tcsrchr(_szPath, _T('\\')))[1] = 0;//删除文件名
for (int n = 0; _szPath[n]; n++){
if (_szPath[n] != _T('\\')){
strPath += _szPath[n];
}
else{
strPath += _T("\\\\");
}
}
GetDlgItem(IDC_EDIT3)->SetWindowTextW(strPath);
strPath = strPath + strFileName;
//ini文件操作
CString strValue, strSection, strSectionKey;
//写入
strSection = "Section1";
strSectionKey = "Item1";
GetDlgItemText(IDC_EDIT1, strValue); //获取第1个文本框内容
////写入ini文件对应的键值
WritePrivateProfileString(strSection, strSectionKey, strValue, strPath);
//读取
TCHAR strBuff[256];
strValue = "";
GetPrivateProfileString(strSection, strSectionKey, NULL, strBuff, 80, strPath);
strValue = strBuff;
GetDlgItem(IDC_EDIT2)->SetWindowTextW(strValue); //读取的值写入第二个文本框