通讯录,创建信息后查找的时候找不到,怎么回事?还有帮忙看看第二个函数和功能问题出在哪?
程序代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define n 4 int k; FILE*fp; //建立日期函数 struct date { unsigned short year; unsigned short month; unsigned short day; }; //建立函数结构体存储信息 struct person { int QQ; int number; //序号 char name[10]; char sex[10]; int telephone; //手机号码 int hometele; //家庭电话 char email[10]; char address[30]; //通讯地址 struct date birthday; }; //编写建立信息文件函数,从键盘输入N个人的信息,并转存到文件中 void mkfile() { FILE*fp; int i; person per[n]; if((fp=fopen("filename","wb"))==NULL) { printf("can not open this file\n"); exit(0); } printf("请输入%d个人的以下信息:\n",n); printf("\n\n\t\t======================================================\n"); for(i=0;i<n;i++) { printf("\t\t第i位同学的学号:",i+1); scanf("%d",&per[i].number); printf("\t\t第%d位同学的姓名:",i+1); scanf("%s",per[i].name); printf("\t\t第%d位同学的性别:",i+1); scanf("%s",per[i].sex); printf("\t\t第%d位同学的电话号码:",i+1); scanf("%d",&per[i].telephone); printf("\t\t第%d位同学的QQ:",i+1); scanf("%d",&per[i].QQ); printf("\t\t第%d同学的家庭号码:",i+1); scanf("%d",&per[i].hometele); printf("\t\t第%d位同学的电子邮件:",i+1); scanf("%s",per[i].address); printf("\t\t第%d位同学的出生日期:",i+1); scanf("%d/%d/%d",&per[i].birthday.year,&per[i].birthday.month,per[i].birthday.day); fwrite(&per[i],sizeof(per[n]),1,fp); if(fwrite(&per[i],sizeof(per),1,fp)!=1) { printf("\n\n\t\t无法打开文件!"); break; } } fclose(fp); printf("\n\n\t\t文件保存成功!请按任意键返回主菜单···"); scanf("%d",&k); return ; } //显示函数 void show() { person per[n]; FILE*fp; int i; if(fread(&per[i],sizeof(per[n]),1,fp)!=1) { printf("无法打开文件!"); exit(0); } else { for(i=0;i<n;i++) { printf("\t\t%d%9s%9s",per[i].number,per[i].name,per[i].sex); printf("\t\t%d\t\t%d\t\t%d",per[i].telephone,per[i].QQ,per[i].hometele); printf("\t\t%9s %9s ",per[i].email,per[i].address); printf("\t\t%d/%d/%d\n",per[i].birthday.year,per[i].birthday.month,per[i].birthday.day); } fclose(fp); printf("\n\n\t\t通讯录信息全部如上!按任意键返回主菜单···"); scanf("%d",&k); return ; } } //建立查找函数 void seekInfo() { char seekname[10]; person per[n]; FILE*fp; int i; if((fp=fopen("filename","rb"))==NULL) { printf("can not open this file"); } else { printf("please input seekname :"); scanf("%s",seekname); for(i=0;i<n;i++) { if(strcmp(seekname,per[i].name)==0) //实现查找功能的关键代码 {printf("%d%s%d%d%s%s",per[i].number ,per[i].name ,per[i].telephone,per[i].hometele,per[i].email,per[i].address); printf("%d/%d/%d",per[i].birthday .year ,per[i].birthday .month ,per[i].birthday.day ); } else { printf("\n\n\t\t查无此人!按任意键返回主菜单···\n"); scanf("%d",&k); return ; } fclose(fp); return ; } } } //建立添加信息的函数 void addInfo() { person per[n]; static int i; FILE*fp; if((fp=fopen("filename","a"))==NULL) { printf("cannot open this file\n"); exit(0); } else { printf("\n\t\t请输入需添加的信息:"); printf("\n\n\t\t*************************************\n"); printf("\t\t请输入学号:"); scanf("%d",&per[i].number); printf("\t\t请输入姓名:"); scanf("%s",per[i].name); printf("\t\t请输入性别:"); scanf("%s",per[i].sex); printf("\t\t请输入电话号码:"); scanf("%d",&per[i].telephone); printf("\t\t请输入QQ号:"); scanf("%d",&per[i].QQ); printf("\t\t请输入家庭电话:"); scanf("%d",&per[i].hometele); printf("\t\t请输入电子邮件:"); scanf("%s",per[i].email); printf("\t\t请输入通讯地址:"); scanf("%s",per[i].address); fwrite(&per[i],sizeof(per[n]),1,fp); i++; fclose(fp); printf("\t\t添加成功!按任意键返回主菜单···"); scanf("%d",&k); return ; } } //建立删除函数 void deletInfo() { FILE*fp; person per[n]; char deletname[10]; int i; int count=n; if((fp=fopen("filename","wb"))==NULL) { printf("can not open this file"); exit(0); } else { printf("请输入需要删除信息的人名:"); scanf("%s",&deletname); for(i=0;i<n;i++) { if(deletname==per[i].name) { per[i]=per[i+1]; //以向前赋值的方法实现个人信息的向前移动,进而删除了第i 个人的信息 count-=1; //通讯录删除某个信息后的人数 printf("删除信息后的通讯录:"); for(i=0;i<count;i++) { printf("\t\t%d\t\t\t %-6s%-3s",per[i].number,per[i].name,per[i].sex); printf("\t\t%d\t\t\t%d\t\t\t%d",per[i].telephone,per[i].QQ,per[i].hometele); printf("%-9s%-9s",per[i].email,per[i].address); printf("\t\t%d/%d/%d\n",per[i].birthday.year,per[i].birthday.month,per[i].birthday.day); } } printf("\n"); } printf("\n\t\t查无此人!按任意键返回主菜单···"); scanf("%d",&k); return ; } } //更新 void update() { FILE*fp; person per[n]; char seekname[10]; int i,k=0; char m; if((fp=fopen("filename","wb"))==NULL) { printf("can not open this file"); exit(0); } else { printf("\n\t\t请输入需修改信息的人名字:"); scanf("%s",seekname); for(i=0;i<n;i++) { if(seekname==per[i].name) { printf("\n\n\t\t%-9s%-9s%-9s%-9s%-9s%-9s%-9s%-9s%-9s\n","学号","姓名","性别","电话号码","QQ号码","家庭电话","电子邮件","通讯地址","出生日期"); printf("\t\t%d\t\t\t %-6s%-3s",per[i].number,per[i].name,per[i].sex); printf("\t\t%d\t\t\t%d\t\t\t%d",per[i].telephone,per[i].QQ,per[i].hometele); printf("%-9s%-9s",per[i].email,per[i].address); printf("\t\t%d/%d/%d\n",per[i].birthday.year,per[i].birthday.month,per[i].birthday.day); printf("\n\n\t\t请选择需修改的信息项:"); printf("\n\t\ta----学号"); printf("\n\t\tb----性别"); printf("\n\t\tc----电话号码"); printf("\n\t\td----QQ号码"); printf("\n\t\te----家庭电话"); printf("\n\t\tf----电子邮件"); printf("\n\t\tg----通讯地址"); printf("\nt\\th----出生日期"); printf("\t\t请选择:"); m=getchar(); switch(m) { case 'a':printf("\n\t\t请输入新的学号:"); scanf("%d",&per[i].number);break; case 'b':printf("\n\t\t请输入新的性别:"); scanf("%s",per[i].sex);break; case 'c':printf("\n\t\t请输入新的电话号码:"); scanf("%d",&per[i].telephone);break; case 'd':printf("\n\t\t请输入新的QQ号码:"); scanf("%d",&per[i].QQ);break; case 'e':printf("\n\t\t请输入新的家庭电话:"); scanf("%d",&per[i].hometele);break; case 'f':printf("\n\t\t请输入新的电子邮件:"); scanf("%s",per[i].email);break; case 'g':printf("\n\t\t请输入新的通讯地址:"); scanf("%s",per[i].address);break; default :printf("\n\t\t选择错误,请重新选择:"); getchar(); fwrite(&per[i],sizeof(per[n]),1,fp); printf("\n\t\t信息更新成功!按任意键返回主菜单···"); scanf("%d",&k); return ; } } else printf("\n\n\t\t查无此人!"); } } } int main() { while(1) { printf("\n\n\t\t*****************************************\n"); printf("\n\n\t\t@¥¥欢迎使用此通讯录管理系统¥¥@\n"); printf("\n"); printf(" 1----创建通讯录\n"); printf(" 2----浏览通讯录\n"); printf(" 3----查找通讯录信息\n"); printf(" 4----添加通讯录信息\n"); printf(" 5----删除通讯录\n"); printf(" 6----更新通讯录\n"); printf(" 7----退出系统\n"); printf("\n\n\t\t******************************************\n"); printf(" 请选择:"); scanf("%d",&k); switch(k) { case 1: mkfile(); continue; case 2:show(); continue; case 3:seekInfo(); continue; case 4:addInfo(); continue; case 5:deletInfo(); continue; case 6:update(); continue; case 7: return 0 ; default:printf("\t\t选择错误!请重新选择:"); scanf("%d",&k); } } return 0; }