这是我同学找到的程序,可以运行,但是删除函数某个地方出错了,可以帮我看看修改下吗?
#include <stdio.h>
#define N 50
struct txl
{
char name[20];
char phone[15];
char addrss[30];
}lxren[N];
FILE *fp;
input();
modify();
add();
del();
display();
search();
main()
{
char t;
while(1)
{
printf(" ************ WELCOME TO USE THIS PROGRAM **********\n");
printf(" * [0]---Input information *\n");
printf(" * [1]---Modify infomation *\n");
printf(" * [2]---Add infomation *\n");
printf(" * [3]---Delete infomation *\n");
printf(" * [4]---Search infomation *\n");
printf(" * [5]---Display all infomation *\n");
printf(" * [6]---Exit *\n");
printf(" *******************************************************\n");
printf(" Please choose(0~6):\n");
flushall();
scanf("%c",&t);
switch(t)
{
case '0':input();break;
case '1':modify();break;
case '2':add();break;
case '3':del();break;
case '4':search();break;
case '5':display();break;
case '6':exit(1);
default :printf("Your prest error!!!Please select again....\n");
getch();break;
}
}
}
input()
{
int i=0;
FILE *fp;
char flag='Y';
if((fp=fopen("list.txt","at+"))==NULL)
{
printf("Can't open this file!!!");
getch();
exit(1);
}
while(flag=='Y'||flag=='y')
{
printf("Name:\n");
scanf("%s",lxren[i].name);
printf("Phone:\n");
scanf("%s",lxren[i].phone);
printf("Addrss:\n");
scanf("%s",lxren[i].addrss);
if(fwrite(&lxren[i],sizeof(struct txl),1,fp)!=1)
{
printf("Error!!!");
getch();
exit(1);
}
else printf("Success!!!\n");
flushall();
printf("Continue Input?(Y/N):");
scanf("%c",&flag);
i++;
}
fclose(fp);
}
modify()
{
struct txl modify_lxren[N],m;
int i,j,t;
char modify_name[10];
if((fp=fopen("list.txt","rt"))==NULL)
{
printf("Open file error!!!\n");
getch();
exit(0);
}
for(i=0;i<N;i++)
{
j=fgetc(fp);
if(j==EOF&&i==0) {printf("This file haven no record!!!\n");getch();return;}
if(j==EOF&&i!=0) break;
fseek(fp,i*sizeof(struct txl),0);
fread(&modify_lxren[i],sizeof(struct txl),1,fp);
}
fclose(fp);
printf("Input the name you will modify:\n");
scanf("%s",modify_name);
for(t=0;t<i;t++)
{
if(strcmp(modify_name,modify_lxren[t].name)==0)
{
printf("Input the info you will modify:\n");
printf("modify name:\n");
scanf("%s",modify_lxren[t].name);
printf("modify phone:\n");
scanf("%s",modify_lxren[t].phone);
printf("modify addrss:\n");
scanf("%s",modify_lxren[t].addrss);
break;
}
}
if((fp=fopen("list.txt","wt"))==NULL)
{
printf("Open file error!!!\n");
getch();
exit(0);
}
for(t=0;t<i;t++)
fwrite(&modify_lxren[t],sizeof(struct txl),1,fp);
printf("Modify Success!!!\n");
getch();
fclose(fp);
}
add()
{
struct txl add_lxren;
if((fp=fopen("list.txt","at+"))==NULL)
{
printf("Open file error!!!\n");
getch();
exit(0);
}
printf("Input the info you will add:\n");
printf("Add name:\n");
scanf("%s",add_lxren.name);
printf("Add phone:\n");
scanf("%s",add_lxren.phone);
printf("Add addrss:\n");
scanf("%s",add_lxren.addrss);
fwrite(&add_lxren,sizeof(struct txl),1,fp);
fclose(fp);
printf("Add Success!!!");
getch();
}
del()
{
struct txl del_lxren[N];
char delete_name[10];
int i,j,t;
if((fp=fopen("list.txt","rt"))==NULL)
{
printf("Open file error!!!\n");
getch();
exit(0);
}
for(i=0;i<N;i++)
{
j=fgetc(fp);
if(j==EOF&&i==0) {printf("This file haven no record!!!\n");getch();return;}
if(j==EOF&&i!=0) break;
fseek(fp,i*sizeof(struct txl),0);
fread(&del_lxren[i],sizeof(struct txl),1,fp);
}
fclose(fp);
printf("Input the name you will delete:\n");
scanf("%s",delete_name);
for(t=0;t<i;t++)
if(strcmp(delete_name,del_lxren[t].name)==0)
{
printf("The info will be delete as follow:\n");
printf("%s %s %s\n",del_lxren[t].name,del_lxren[t].phone,del_lxren[t].addrss);
getch();
break;
}
for(j=t;j<t-1;j++)
del_lxren[j]=del_lxren[j+1];
if((fp=fopen("list.txt","w"))==NULL)
{
printf("Open file error!!!\n");
getch();
exit(0);
}
for(j=0;j<i;j++)
fwrite(&del_lxren[j],sizeof(struct txl),1,fp);
printf("\nDelete Success!!!\n");
fclose(fp);
getch();
}
search()
{
int i,j,t;
char search_name[10];
struct txl search_lxren[N];
if((fp=fopen("list.txt","rt"))==NULL)
{
printf("Open file error!!!\n");
getch();
exit(0);
}
for(i=0;i<N;i++)
{
j=fgetc(fp);
if(j==EOF&&i==0) {printf("This file haven no record!!!\n");getch();return;}
if(j==EOF&&i!=0) break;
fseek(fp,i*sizeof(struct txl),0);
fread(&search_lxren[i],sizeof(struct txl),1,fp);
}
if(i==N) {printf("No any records!!!"); getch(); exit(0);}
fclose(fp);
printf("Input the name you will search:\n");
scanf("%s",search_name);
for(t=0;t<=i;t++)
if(strcmp(search_name,search_lxren[t].name)==0)
{
printf("The info you will search as follow:\n");
printf("%s %s %s\n",search_lxren[t].name,search_lxren[t].phone,search_lxren[t].addrss);
getch();
return;
}
}
display()
{ int i;
struct txl dis_lxren[N];
int j;
if((fp=fopen("list.txt","at+"))==NULL)
{
printf("Open file error!!!");
getch();
exit(0);
}
printf("Name Phone Addrss\n");
printf("--------------------------------------------------------\n");
for(i=0;i<N;i++)
{
j=fgetc(fp);
if(j==EOF&&i==0) {printf("This file haven no record!!!\n");getch();return;}
if(j==EOF&&i!=0) break;
fseek(fp,i*sizeof(struct txl),0);
fread(&dis_lxren[i],sizeof(struct txl),1,fp);
printf("%s %s %s \n",dis_lxren[i].name,dis_lxren[i].phone,dis_lxren[i].addrss);
flushall();
}
fclose(fp);
}