用VC6.0 mfc创建对话框项目文件EditWriteIni,在资源视图上拖放两个Editr Box控件IDC_EDIT1(写入数据)、IDC_EDIT2(读出数据),两个Button控件IDC_BUTTON1(写入INI文件)、IDC_BUTTON2(读出INI文件)。
写入INI文件按钮单击事件代码:
void CEditWriteIniDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString strTemp;
char strBuff[256];
CString strFilePath;
strFilePath=GetCurrentDirectory(256,strBuff); //获取当前路径
strFilePath.Format("%s//Test.ini",strBuff);
GetDlgItem(IDC_EDIT1)->GetWindowText(strTemp);//获取当前内容
WritePrivateProfileString("差压传感器","校零电压",strTemp,strFilePath);
}
读出INI文件按钮单击事件代码:
void CEditWriteIniDlg::OnButton2()
{
// TODO: Add your control notification handler code here
CString strTemp;//数据项名称
char strBuff[256];
CString strFilePath;
strFilePath=GetCurrentDirectory(256,strBuff); //获取当前路径
strFilePath.Format("%s//Test.ini",strBuff);
GetPrivateProfileString("差压传感器","校零电压",NULL,strTemp.GetBuffer(20),20,strFilePath);
GetDlgItem(IDC_EDIT2)->SetWindowText(strTemp);
}
运行程序在IDC_EDIT1输入数据,单击写入INI文件按钮,数据写入当前项目文件下test.ini文件中,单击讲读出INI文件按钮,IDC_EDIT2显示写入INI文件数据。
源码下载
https://download.csdn.net/download/weixin_42350092/87700786