⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 del_user.cpp

📁 数据结构大作业
💻 CPP
字号:
// del_user.cpp : implementation file
//

#include "stdafx.h"
#include "salsary.h"
#include "del_user.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// del_user dialog


del_user::del_user(CWnd* pParent /*=NULL*/)
	: CDialog(del_user::IDD, pParent)
{
	//{{AFX_DATA_INIT(del_user)
	m_user = _T("");
	m_name = _T("");
	m_salary = _T("");
	m_key = _T("");
	m_age = _T("");
	//}}AFX_DATA_INIT
}


void del_user::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(del_user)
	DDX_Control(pDX, IDC_LIST1, m_List);
	DDX_Text(pDX, IDC_user, m_user);
	DDV_MaxChars(pDX, m_user, 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_key, m_key);
	DDV_MaxChars(pDX, m_key, 10);
	DDX_Text(pDX, IDC_age, m_age);
	DDV_MaxChars(pDX, m_age, 10);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(del_user, CDialog)
	//{{AFX_MSG_MAP(del_user)
	ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// del_user message handlers
#include"wh.h"
void del_user::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"
BOOL del_user::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
}

void del_user::OnButton2() 
{
EndDialog(1);	// TODO: Add your control notification handler code here
	
}
#include"wh.h"
void del_user::OnButton1()//在列表框和文件中删除数据 

{int t=MessageBox("确认要删除信息?","注意!",4+32+256);
 if(t==6)
 {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;
    node*head,*ps,*pEnd; 
    head=NULL;
	ps=new node;
	pEnd=ps;
    do{iof.read((char*)&data,sizeof(user_data));
    user=data.user; 
    if(m_user.Compare(user))
    {{strcpy(ps->name,data.name);strcpy(ps->user,data.user);strcpy(ps->key,data.key);
	ps->age=data.age;ps->salary=data.salary;}
	if(head==NULL)
    head=ps;
    else {pEnd->next=ps;}
    pEnd=ps;
    ps=new node;
	}}while(iof.tellp()!=posEnd);
	pEnd->next=NULL;
	delete ps;
    iof.close();
	_unlink("C:\\user.dat");//删除数据文件
    fstream f("C:\\user.dat",ios::binary|ios::out|ios::in);
    if(!f)
	{cerr<<"文件不能打开"<<endl;
    return ;
	} 
	f.seekp(0,ios::beg);
    while(head!=NULL)
    {{strcpy(data.name,head->name);strcpy(data.user,head->user);
	strcpy(data.key,head->key);data.age=head->age;data.salary=data.salary;}
    f.write((char*)&data,sizeof(user_data));
	head=head->next;}
 }
   	delete(user_data*)m_List.GetItemDataPtr(nIndex);
    m_List.DeleteString(nIndex);
	m_user="";
	m_name="";
	m_age="";
	m_salary="";
	m_key="";
	UpdateData(FALSE);}
	else MessageBox("在列表框中不存在或操作失败!");
	
	
		// TODO: Add your control notification handler code here
}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -