怎么样才能让这段代码在删除内容时 做到 输入的数字不在我所规定的范围内和不在文本中或不存在 就退出程序呢?也就是容错处理!!~ 还有 我要把删除的号码改成姓名 怎么改啊?我老改不对!!~请教高手!!~ #include<stdio.h> #include<process.h> #include<malloc.h> #include<string.h>
void add_tel(); //添加新订户 void modify_existing(); //修改现有订户的详细信息 void del_existing(); //删除现有订户的详细信息 void telephone(); //根据电话号码查出的订户详细信息 void subscriber(); //根据订户名查出的订户详细信息 void view_Directory(); //查看目录 void sort();
int choice=1;
struct aaa{ char name[30]; char address[50]; //地址 int telhome; };
void main() { printf(" TELEPHONE DIRECTORY SYSTEM\n"); int choice; while(choice!=7) { system("cls"); printf("\nSelect choice from meun\n\n1. Add new subscriber details\n"); printf("2. Modify existing subscriber details\n"); printf("3. Delete existing subscriber details\n"); printf("4. Display subscriber details based on telephone number\n"); printf("5. Display subscriber details based on subscriber name\n"); printf("6. View Directory\n"); printf("7. Exit\n"); printf("\nEnter choice:"); scanf("%d",&choice); fflush(stdin); if(choice==1) add_tel(); else if(choice==2) modify_existing(); else if(choice==3) del_existing(); else if(choice==4) telephone(); else if(choice==5) subscriber(); else if(choice==6) view_Directory(); } }
/*添加新订户*/ void add_tel() { char flag='y'; char find='n'; struct aaa tel; FILE *fp; if((fp=fopen("f:\\telefon.txt","a+b"))==NULL)//判断打开telefon.txt文本是否成功 { exit(0);//退出整個程序 } system("cls");//清屏 while(flag=='y') { printf("\tADD NEW SUBSCRIBER DETAILS\n\n"); printf("请输入姓名<upto 30 chars.>:"); scanf("%s",tel.name); printf("请输入详细地址<upto 50 chars.>:"); scanf("%s",tel.address); printf("请输入电话号码:"); scanf("%d",&tel.telhome); fwrite(&tel,sizeof(struct aaa),1,fp); while(tel.telhome<4000000||tel.telhome>4999999) { printf("\n\tSubscriber telephone number must be between 4000000 and 4999999\n"); printf("\n请重新输入电话号码:"); scanf(" %d",&tel.telhome); } printf("添加成功!\n是否继续添加?(y/n):"); scanf(" %c",&flag); system("cls"); } fclose(fp); getchar(); }
/*修改现有订户的详细信息*/ void modify_existing() { FILE *fp; struct aaa tel; char flag='y'; char name[30]; if((fp=fopen("f:\\telefon.txt","r+b"))==NULL) { printf("打开文件出错!!"); //The system can't open the file getchar(); return; } system("cls"); { printf("\n\tMODIFY SUBSCRIBER DETAILS"); printf("\n\n请输入您要修改的用户名 <upto 30 chars.>:"); scanf("%s",name); rewind(fp); system("cls"); while((fread(&tel,sizeof(struct aaa),1,fp))==1) { if(strcmp(name,tel.name)==0) { printf("\n\tRECORD TO BE MODIFIED\n"); flag='y'; break; } } if(flag=='y') {
printf("姓名:%s\n",tel.name); printf("地址:%s\n",tel.address); printf("电话号码:%d\n",tel.telhome);
printf("\nENTER NEW RECORD DETAILS\n\n"); printf("请输入新的姓名:"); scanf("%s",tel.name); printf("请输入新的地址:"); scanf("%s",tel.address); printf("请输入新的电话号码:"); scanf("%d",&tel.telhome);
fseek(fp,-(long)(sizeof(struct aaa)),1); fwrite(&tel,sizeof(struct aaa),1,fp); } printf("是否继续修改(y/n):"); scanf(" %c",&flag); } fclose(fp); }
/*删除现有订户的详细信息*/ void del_existing() { FILE *fp; struct aaa tel; struct aaa *bbb;
char newtelhome,stuCount=0,i=0; fp=fopen("f:\\telefon.txt","rb"); while(fread(&tel,sizeof(struct aaa),1,fp)==1) { stuCount++; }
bbb=(struct aaa *)malloc((stuCount-1)*sizeof(struct aaa));
printf("请输入要删除的电话号码:"); scanf("%d",&newtelhome); rewind(fp); while(fread(&tel,sizeof(struct aaa),1,fp)==1) { if(tel.telhome!=newtelhome) { bbb[i]=tel; i++; } } fclose(fp); fp=fopen("f:\\telefon.txt","wb"); fwrite(bbb,sizeof(struct aaa),stuCount-1,fp); fclose(fp); printf("删除完毕!\n"); getchar(); getchar();
}
/*根据电话号码查出的订户详细信息*/ void telephone() { char flag='y'; int newtelhome; FILE *fp; struct aaa tel; if((fp=fopen("f:\\telefon.txt","r+b"))==NULL) { printf("打开文件出错!!"); getchar(); return; } system("cls"); while(flag=='y') { system("cls"); printf("\tDisplay subscriber details based on telephone number\n\n"); printf("请输入您要查的电话号码:"); scanf("%d",&newtelhome);
rewind(fp); while((fread(&tel,sizeof(struct aaa),1,fp))==1) { if(tel.telhome==newtelhome) { printf("\n\tRECORD TO BE MODIFIED\n\n"); flag='y'; break; } } if(flag=='y') { printf("姓名:%s\n",tel.name); printf("地址:%s\n",tel.address); printf("电话号码:%d\n",tel.telhome); } printf("是否继续以电话号码查询?(y/n)"); scanf(" %c",&flag); } fclose(fp); getchar(); }
/*根据订户名查出的订户详细信息*/ void subscriber() { FILE *fp; char flag='y'; struct aaa tel; char newname[30]; if((fp=fopen("f:\\telefon.txt","r+b"))==NULL) { printf("打开文件出错!!"); getchar(); return; } system("cls"); while(flag=='y') { system("cls"); printf("\t\n\n"); printf("请输入您要查的姓名:"); scanf("%s",newname); rewind(fp); while((fread(&tel,sizeof(struct aaa),1,fp))==1) { system("cls"); printf("\n\tRECORD TO BE MODIFIED\n\n"); if(strcmp(newname,tel.name)==0) { flag='y'; break; } } if(flag=='y') { printf("姓名:%s\n",tel.name); printf("地址:%s\n",tel.address); printf("电话号码:%d\n",tel.telhome); } printf("是否继续以姓名查询?(y/n)"); scanf(" %c",&flag); } fclose(fp); }
//查看目录 void view_Directory() { sort(); FILE *fp; struct aaa temp; int i;
if((fp=fopen("f:\\telefon.txt","r+b"))==NULL) { printf("打开文件出错!!"); getchar(); return; } fp=fopen("f:\\telefon.txt","rb"); while(fread(&temp,sizeof(struct aaa),1,fp)==1) { printf("Record 姓名:%-15s 地址:%-12s 电话号码:%-15d\n",temp.name,temp.address,temp.telhome); i++; } fclose(fp); printf("显示完毕!!\n\n"); printf("请按回车键返回主菜单!!"); getchar(); }
//排序 void sort()
{ FILE *fp; struct aaa tel,temp,temp1; fp=fopen("f:\\telefon.txt","r+b"); int count=0; while(fread(&tel,sizeof(struct aaa),1,fp)==1) { count++; } int i,j; for(i=0;i<count-1;i++) { for(j=0;j<count-i-1;j++) { fseek(fp,j*sizeof(struct aaa),0); fread(&temp,sizeof(struct aaa),1,fp); fseek(fp,(j+1)*sizeof(struct aaa),0); fread(&temp1,sizeof(struct aaa),1,fp); if(strcmp(temp.name,temp1.name)>0) { fseek(fp,j*sizeof(struct aaa),0); fwrite(&temp1,sizeof(struct aaa),1,fp); fseek(fp,(j+1)*sizeof(struct aaa),0); fwrite(&temp,sizeof(struct aaa),1,fp); } } } fclose(fp); }
[此贴子已经被作者于2005-9-11 23:21:42编辑过]