void creat_record(void) { char ch; printf("-------------This is a new record!-------------\n"); printf("Please Enter Name:\n"); gets(stu[n].name); printf("Please Enter Age:\n"); gets(stu[n].age); printf("Please Enter Sex:\n"); gets(stu[n].sex); printf("Please Enter Room:\n"); gets(stu[n].room); printf("Please Enter ID:\n:"); gets(stu[n].ID); printf("Please Enter Phone_num:\n"); gets(stu[n].phone_num); n++; printf("Do you want to save ? Please press 'Y' or 'N'...\n"); ch=getchar(); getchar(); if(ch=='y'||ch=='Y') wfile(); if(ch=='n'||ch=='N') return; } void list_record(void) { int i=1; for(;i<=n;i++) { printf("=============Record %d==================\n",i); printf("Name:%s\n",stu[i].name); printf("Age:%s\n",stu[i].age); printf("Sex:%s\n",stu[i].sex); printf("room:%s\n",stu[i].room); printf("ID:%s\n",stu[i].ID); printf("Phone_num:%s\n",stu[i].phone_num); } }
void search_record(void) { int k=1; char name2[15]; printf("Please Enter the person name:\n:"); gets(name2); for(;k<=n;k++) if(strcmp(name2,stu[k].name)==0) { printf("Name:%s\n",stu[k].name); printf("Age:%s\n",stu[k].age); printf("Sex:%s\n",stu[k].sex); printf("room:%s\n",stu[k].room); printf("ID:%s\n",stu[k].ID); printf("Phone_num:%s\n",stu[k].phone_num); } }
void wfile(void) { FILE *fp;
if((fp=fopen("phonebook","wb"))==NULL) printf("Can not open file!\n"); else { fwrite(&stu[n],sizeof(stu[0]),n,fp); fclose(fp); printf("Has been saved!\n"); } }
printf("=================Welcome to the phonebook!=================\n"); while(a) { printf("Press E to creat a new record\n"); printf("Press L to list the all record\n"); printf("Press S to search the record\n"); printf("Press C to cannle the programme\n"); ch=getchar(); getchar(); switch(ch) { case 'e': case 'E':creat_record();break; case 'l': case 'L':list_record();break; case 's': case 'S':search_record();break;