📄 ad_modify.cpp
字号:
// ad_modify.cpp : implementation file
//
#include "stdafx.h"
#include "salsary.h"
#include "ad_modify.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// ad_modify dialog
ad_modify::ad_modify(CWnd* pParent /*=NULL*/)
: CDialog(ad_modify::IDD, pParent)
{
//{{AFX_DATA_INIT(ad_modify)
m_age = _T("");
m_key = _T("");
m_name = _T("");
m_salary = _T("");
m_user = _T("");
//}}AFX_DATA_INIT
}
void ad_modify::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(ad_modify)
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Text(pDX, IDC_age, m_age);
DDV_MaxChars(pDX, m_age, 10);
DDX_Text(pDX, IDC_key, m_key);
DDV_MaxChars(pDX, m_key, 10);
DDX_Text(pDX, IDC_name, m_name);
DDV_MaxChars(pDX, m_name, 10);
DDX_Text(pDX, IDC_salary, m_salary);
DDV_MaxChars(pDX, m_salary, 10);
DDX_Text(pDX, IDC_user, m_user);
DDV_MaxChars(pDX, m_user, 10);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(ad_modify, CDialog)
//{{AFX_MSG_MAP(ad_modify)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// ad_modify message handlers
void ad_modify::OnButton2()
{
EndDialog(1); // TODO: Add your control notification handler code here
}
#include"wh.h"
BOOL ad_modify::OnInitDialog()
{
CDialog::OnInitDialog();
fstream iof("C:\\user.dat",ios::binary|ios::in|ios::out);
if(!iof)
{cerr<<"文件不能打开"<<endl;
return FALSE;
}
user_data data;
iof.seekp(0,ios::end);
long posEnd=iof.tellp();//记录末尾指针的位置
iof.seekp(0,ios::beg);
do
{iof.read((char*)&data,sizeof(user_data));
m_user=data.user;
m_key=data.key;
m_name=data.name;
m_age.Format("%d",data.age);
m_salary.Format("%d",data.salary);
int nIndex=m_list.AddString(m_user);//向列表框添加数据
strcpy((data.key),(LPSTR)(LPCTSTR)(m_key));
strcpy((data.name),(LPSTR)(LPCTSTR)(m_name));
(data.age)=atoi(m_age);
(data.salary)=atoi(m_salary);
m_list.SetItemDataPtr(nIndex,new user_data(data));
UpdateData(FALSE); }
while(iof.tellp()!=posEnd);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
#include"wh.h"
void ad_modify::OnSelchangeList1()
{
int nIndex=m_list.GetCurSel();
if(nIndex!=LB_ERR)
{m_list.GetText(nIndex,m_user);
user_data*data=(user_data*)m_list.GetItemDataPtr(nIndex);
m_key=(*data).key;
m_name=(*data).name;
m_age.Format("%d",(*data).age);
m_salary.Format("%d",(*data).salary);
UpdateData(FALSE);}
// TODO: Add your control notification handler code here
}
#include"wh.h"
void ad_modify::OnButton1()
{int t=MessageBox("是否要进行修改?","提示!",4+32);
if(t==6)
{UpdateData();
int nIndex=m_list.GetCurSel();
if(nIndex!=LB_ERR)
{fstream iof("C:\\user.dat",ios::binary|ios::in|ios::out);
if(!iof)
{cerr<<"文件不能打开"<<endl;
return;
}
user_data data;
iof.seekp(0,ios::end);
long posEnd=iof.tellp();
iof.seekp(0,ios::beg);
CString user;
do
{iof.read((char*)&data,sizeof(user_data));
user=data.user;
}while(m_user.Compare(user)&&iof.tellp()!=posEnd);
if(!m_user.Compare(user))
{strcpy((data.name),(LPSTR)(LPCTSTR)(m_name));
(data.age)=atoi(m_age);
(data.salary)=atoi(m_salary);
m_list.SetItemDataPtr(nIndex,new user_data(data));
iof.seekp(-long(sizeof(user_data)),ios::cur);//文件指针复位
iof.write((char*)&data,sizeof(user_data));
MessageBox("修改用户资料成功!");
iof.close();}
// TODO: Add your control notification handler code here
}}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -