C++ ini文件的读取与写入

时间:2022-11-28 04:31:24

//读取.ini文件中的内容

BOOL CDlgCamIP::OnInitDialog()

{
CDialog::OnInitDialog();
//读取配置文件信息
CString dir = GetModuleDir()+"\\client.ini";
memset(&g_netInfo, 0, sizeof(g_netInfo));
GetPrivateProfileString("camerainfo","cameraip", "", g_netInfo.Camera_IP, sizeof(g_netInfo.Camera_IP), dir);
GetPrivateProfileString("localinfo", "localip", "", g_netInfo.Local_IP, sizeof(g_netInfo.Local_IP), dir);
// 将从文件中取出来的值放入编辑框中
GetDlgItem(IDC_IPADDRCAMIP)->SetWindowText(g_netInfo.Camera_IP);
return TRUE;  // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE

}

//在编辑框中修改IP的值,点击确定后,改变后的IP值写入文件中

void CDlgCamIP::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
//读取配置文件信息
CString dir = GetModuleDir()+"\\client.ini";
//获取编辑框中的IP地址
DWORD dwAddress ;
m_editCameraIP.GetAddress(dwAddress);
CString str;
str.Format("%d.%d.%d.%d",(0xFF000000&dwAddress)>>24,(0xFF0000&dwAddress)>>16,(0xFF00&dwAddress)>>8,0xFF&dwAddress);
//将IP地址写入编辑框中
WritePrivateProfileString("camerainfo","cameraip", str,dir);
strcpy(g_netInfo.Camera_IP, str);
TRACE("%s\n", g_netInfo.Camera_IP);
CompareIP(m_pCamIpBuf, m_pLocIpBuf);
WritePrivateProfileString("localinfo", "localip", g_netInfo.Local_IP, dir);
OnOK();
}