求助,小型通讯录管理系统中的删除和修改怎么都弄不好,求大神指点。
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <windows.h>
struct stu
{
char name[30];
char tel[30];
char email[30];
char job[30];
}stu[200];
int mm=0;
int s1=0;
int s2=0;
int s3=0;
int s4=0;
int s5=0;
int bz=0;
int f=0;
int k;
int i=0;
void read()
{
FILE *fp;
if((fp=fopen("D:\\通讯录.txt","r"))==NULL)
{
printf("file cannot open!\n");
}
int i=0;
while(!feof(fp))
{
fread(&stu[i],sizeof(struct stu),1,fp);
i++;
}
for(k=0;k<i-1;k++)
{
printf("姓名:%s\t电话:%s\t邮箱:%s\t职务:%s\n",stu[k].name,stu[k].tel,stu[k].email,stu[k].job);
}
fclose(fp);
}
void add()
{
FILE* fp=fopen("D:\\通讯录.txt","a+");
int n;
struct stu stu[200];
printf("新建联系人个数:");
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
{
printf("\t电话:");
scanf("%s",&stu[i].tel);
printf("\t姓名:");
scanf("%s",stu[i].name);
printf("\t邮箱:");
scanf("%s",&stu[i].email);
printf("\t职务:");
scanf("%s",&stu[i].job);
fwrite(&stu[i],sizeof(struct stu),1,fp);
mm++;
}
fclose(fp);
}
void listtop()
{
FILE *fp;
if((fp=fopen("D:\\通讯录.txt","r"))==NULL)
{
printf("file cannot open!\n");
}
int i=0;
while(!feof(fp))
{
fread(&stu[i],sizeof(struct stu),1,fp);
i++;
}
for(k=0;k<i-1;k++)
{
printf("姓名:%s\t电话:%s\n",stu[k].name,stu[k].tel);
}
fclose(fp);
}
void list()
{
printf(" | | \n");
printf(" | 1.查询联系人 | \n");
printf(" | 2.编辑联系人 | \n");
printf(" | 3.显示全部信息 | \n");
printf(" | 4.退出通讯录 | \n");
}
void listcx()
{
printf(" | 查询联系人 | \n");
printf(" | | \n");
printf(" | 1.按姓名查找 | \n");
printf(" | 2.按电话查找 | \n");
printf(" | 3.按邮箱查找 | \n");
printf(" | 4.按职务查找 | \n");
printf(" | 5.返回上一级菜单 | \n");
}
void listbj()
{
printf(" | 编辑联系人 | \n");
printf(" | | \n");
printf(" | 1.添加联系人 | \n");
printf(" | 2.修改联系人 | \n");
printf(" | 3.删除联系人 | \n");
printf(" | 4.返回上一级菜单 | \n");
printf(" | | \n");
}
int main()
{
struct stu stu[200];
K: printf(" 通 讯 录 \n");
printf("=======================================================================\n");
listtop();
printf("=======================================================================\n");
G: list();
int xz1;
scanf("%d",&xz1);
switch(xz1)
{
case 1: //查询
L: system("cls");
listcx();
int xz2;
scanf("%d",&xz2);
switch(xz2)
{
case 1:
FILE *fp;
fp=fopen("D:\\通讯录.txt","r");
int a,xz4;
char aname[20];
printf("姓名:");
scanf("%s",&aname);
for(a=0;a<200;a++)
{
fread(&stu[a],sizeof(struct stu),1,fp);
if(strcmp(aname,stu[a].name)==0)
{
s1++;
system("cls");
printf("姓名:%s 电话:%s 邮箱:%s 职务:%s\n",stu[a].name,stu[a].tel,stu[a].email,stu[a].job);
}
}
if(s1==0)
{
system("cls");
printf("查无此人\n");
}
break;
case 2:
FILE *fp1;
fp=fopen("D:\\通讯录.txt","r+");
int a1,xz5;
char atel[20];
fflush(stdin);
printf("电话:");
scanf("%s",&atel);
for(a1=0;a1<200;a1++)
{
fread(&stu[a1],sizeof(struct stu),1,fp);
if(strcmp(atel,stu[a1].tel)==0)
{
s2++;
system("cls");
printf("姓名:%s 电话:%s 邮箱:%s 职务:%s\n",stu[a1].name,stu[a1].tel,stu[a1].email,stu[a1].job);
}
}
if(s2==0)
{
system("cls");
printf("查无此人\n");
}
break;
case 3:
FILE *fp2;
fp=fopen("D:\\通讯录.txt","r+");
int a2,xz6;
char aemail[20];
fflush(stdin);
printf("邮箱:");
scanf("%s",&aemail);
for(a2=0;a2<200;a2++)
{
fread(&stu[a2],sizeof(struct stu),1,fp);
if(strcmp(aemail,stu[a2].email)==0)
{
s3++;
system("cls");
printf("姓名:%s 电话:%s 邮箱:%s 职务:%s\n",stu[a2].name,stu[a2].tel,stu[a2].email,stu[a2].job);
}
}
if(s3==0)
{
system("cls");
printf("查无此人\n");
}
break;
case 4:
FILE *fp3;
fp=fopen("D:\\通讯录.txt","r+");
int a3,xz7;
char ajob[20];
fflush(stdin);
printf("职务:");
scanf("%s",&ajob);
for(a3=0;a3<200;a3++)
{
fread(&stu[a3],sizeof(struct stu),1,fp);
if(strcmp(ajob,stu[a3].job)==0)
{
s4++;
system("cls");
printf("姓名:%s 电话:%s 邮箱:%s 职务:%s\n",stu[a3].name,stu[a3].tel,stu[a3].email,stu[a3].job);
}
}
if(s4==0)
{
system("cls");
printf("查无此人\n");
}
break;
case 5:
system("cls");
goto K;
default:
goto L;
}
Z: int xz8;
printf("==============================================\n");
printf(" 1.返回 \n");
scanf("%d",&xz8);
if(xz8==1)
{
system("cls");
goto K;
}
else
{
system("cls");
goto Z;
}
case 2: //编辑
M: system("cls");
listbj();
int xz3;
int v;
scanf("%d",&xz3);
switch(xz3)
{
case 1: //添加
add();
system("cls");
goto K;
case 2: //修改
system("cls");
FILE *fp;
fp=fopen("D:\\通讯录.txt","r+");
char xtel[30];
char xemail[30];
char xjob[30];
char cname[30];
char xname[30];
int cc;
printf("姓名:");
scanf("%s",&cname);
for(cc=0;cc<200;cc++)
{
fread(&stu[cc],sizeof(struct stu),1,fp);
if(strcmp(cname,stu[cc].name)==0)
{
printf("修改姓名为:");
scanf("%s",&xname);
strcpy(stu[cc].name,xname);
printf("修改电话为:");
scanf("%s",&xtel);
strcpy(stu[cc].tel,xtel);
printf("修改邮箱为:");
scanf("%s",&xemail);
strcpy(stu[cc].email,xemail);
printf("修改职务为:");
scanf("%s",&xjob);
strcpy(stu[cc].job,xjob);
fwrite(&stu[cc],sizeof(struct stu),1,fp);
bz++;
}
}
if(bz==0)
{
system("cls");
printf("无此联系人\n");
}
break;
case 3: //删除
system("cls");
FILE *fp1;
fp1=fopen("D:\\通讯录.txt","r+");
int aa;
char bname[30];
int c;
printf("姓名:");
scanf("%s",&bname);
for(c=0;c<200;c++)
{
fread(&stu[c],sizeof(struct stu),1,fp);
if(strcmp(bname,stu[c].name)==0)
{
strcpy(stu[c].name,stu[c+1].name);
strcpy(stu[c].tel,stu[c+1].tel);
strcpy(stu[c].email,stu[c+1].email);
strcpy(stu[c].job,stu[c+1].job);
fwrite(&stu[c],sizeof(struct stu),1,fp);
s5++;
}
}
if(s5==0)
printf("无此联系人\n");
printf("删除成功\n");
break;
case 4:
system("cls");
goto K;
break;
default:
goto M;
}
int xz9;
printf("==============================================\n");
O: printf("1.返回 \n");
scanf("%d",&xz9);
if(xz9==1)
{
system("cls");
goto M;
}
else
{
system("cls");
goto O;
}
case 3:
system("cls");
printf(" 通 讯 录 \n");
printf("=======================================================================\n");
read();
printf("=======================================================================\n");
goto G;
case 4: //退出
exit(0);
default:
system("cls");
goto K;
}
return 0;
}