高手看看哪儿出错了 通讯录代码
这通讯录代码 编译没错 查询时无论怎么输入都显示的是第一行的内容修改函数修改不了内容 删除函数只能删除姓名 电话 QQ之类的 还是保留着
求高手指教
不懂的勿扰
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
struct student
{
char xingming[45];
char xingbie[5];
char dianhua[13];
char qq[13];
char email[30];
}xx,XX;
int printf1()
{
int i;
printf("---------------------------------通讯录");
printf("----------------------------------\n");
printf(" 1显示\n");
printf(" 2添加\n");
printf(" 3修改\n");
printf(" 4查找\n");
printf(" 5删除\n");
printf(" 6退出\n");
printf("我要选择:");
scanf("%d",&i);return i;
}
add()
{
int a;
FILE *fp;
if((fp=fopen("xinxi.txt","a+"))==NULL)
{
printf("error....\n");
getch();
}
printf("输入姓名:");
gets(xx.xingming);
system("cls");
printf("输入姓名:");
gets(xx.xingming);
printf("输入性别:");
gets(xx.xingbie);
printf("输入电话:");
gets(xx.dianhua);
printf("输入QQ:");
gets(xx.qq);
printf("输入eiail:");
gets(xx.email);
printf("\n1.返回--2.添加--3.退出 4baocun");
scanf("%d",&a);
if(a==1)
{
main();
}
else if(a==2)
{
add();
}
else if(a==3)
{
exit(0);
}
else if(a==4)
{
save();
}
}
show()
{
int i;
FILE *fp;
if((fp=fopen("xinxi.txt","rb"))==NULL)
{
printf("error....\n");
getch();
main();
}
while(fread(&xx,sizeof(xx),1,fp)==1)
{
printf("姓名:%s\n",xx.xingming);
printf("性别:%s\n",xx.xingbie);
printf("电话:%s\n",xx.dianhua);
printf("qq:%s\n",xx.qq);
printf("email:%s\n",xx.email);
}
fclose(fp);
printf("1.返回 2.退出");
scanf("%d",&i);
if(i==1)
{
main();
}
else if(i==2)
{
exit(0);
}
else
{
getch();
show();
}
}
update()
{
FILE *fp;
int m,k=0;
long offset;
printf("\n输入姓名:");
scanf("%s",XX.xingming);
fp=fopen("xinxi.txt","r+");
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%-11s%-10s%-10s%-15s%-15s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
if(!strcmp(XX.xingming,xx.xingming))
{
k=1;
break;
}
}
if(k)
{
printf("\n记录为:\n");
printf("\n%-11s%-10s%-10s%-15s%-15s","xingming","xingbie","dianhua","qq","email");
printf("\n%-11s%-10s%-10s%-15s%-15s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
printf("\n输入新姓名 性别 电话 qq email:");
scanf("%-11s%-10s%-10s%-15s%-15s",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
fseek(fp,offset,SEEK_SET);
printf("%ld",ftell(fp));
fprintf(fp,"%-11s%-10s%-10s%-15s%-15s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
}
else
printf("\n没此人。");
fclose(fp);
}
search()
{
FILE *fp;
int k=0;
printf("\n输入姓名:");
scanf("%s",XX.xingming);
fp=fopen("xinxi.txt","rb");
while(!feof(fp)) /*文件检测未结束时进行循环*/
{
fscanf(fp,"%s%s%s%s%s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
if(strcmp(XX.xingming,xx.xingming)) /*输入的姓名和已有的比较*/
{
printf("\n\n记录为:\n");
printf("%-11s%-10s%-10s%-15s%-15s\n","xingming","xingbie","dianhua","qq","email");
printf("\n%-11s%-10s%-10s%-15s%-15s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
k=1;
}
}
if(!k)
printf("\n\n没人。");
fclose(fp);
}
del()
{
FILE *fp;
int m,k=0,flag;
long offset1;
char valid[4];
printf("\n请输入姓名:");
scanf("%s",XX.xingming);
fp=fopen("xinxi.txt","r+");
while(!feof(fp))
{
offset1=ftell(fp);
fscanf(fp,"%s%s%s%s%s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
if(!strcmp(XX.xingming,xx.xingming))
{
k=1;
break;
}
}
if(k)
{
printf("\n查到的记录为:");
printf("\n%-11s%-10s%-10s%-15s%-15s\n","姓名","性别","电话","qq","email");
printf("%-11s%-10s%-10s%-15s%-15s\n",xx.xingming,xx.xingbie,xx.dianhua,xx.qq,xx.email);
printf("\n删除按1,不删按0:");
scanf("%d",&m);
if(m)
{
fseek(fp,offset1,SEEK_SET);/*将fp所指向设置到offset1*/
fprintf(fp,"%-11s%-10s%-16s%-12s\n","","","",""); //为字符块的和
}
}
else printf("\n对不起,通讯簿中没有此人的记录。");
fclose(fp);
}
save()
{
FILE *fp;
int i;
if((fp=fopen("xinxi.txt","a+"))==NULL)
{
printf("error");
return;
}
if(fwrite(&xx,sizeof(struct student),1,fp)!=1)
printf("file write error");
fclose(fp);
main();
}
main()
{
switch(printf1()){
case 1:show();break;
case 2:add();break;
case 3:update();break;
case 4:search();break;
case 5:del();break;
case 6:exit(0);
}
}