读取文件时只能读取一个节点,文件里超过一个,读取的时候就会产生一个访问违例(段异常),请大佬们帮忙看看!!
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct book)
struct book
{
char name[20];
long int number;
struct book *next;
};
struct book *add(struct book *head)
{
struct book *p,*t;
p=(struct book*)malloc(LEN);
printf("请输入您要增加的联系人的名字:");
scanf("%s",p->name);
printf("请输入号码:");
scanf("%ld",&p->number);
p->next=NULL;
t=head;
if(head==NULL)
{
head=p;
return(head);
}
else if(strcmp(p->name,head->name)<0)
{
p->next=head;
head=p;
return(head);
}
else if (t->next!=NULL)
do
{if(strcmp(t->name,p->name)<0&&strcmp((t->next)->name,p->name)>0)
{
p->next=t->next;
t->next=p;
break;
}
t=t->next;
}while(t->next!=NULL);
if( t->next==NULL)
t->next=p;
return(head);
}
void print(struct book * head)
{
if(head==NULL)
{
printf("您已经帅到没朋友了!\n");
}
while(head!=NULL)
{
printf("%s %ld\n",head->name,head->number);
head=head->next;
}
}
struct book *del(struct book *head)
{
if(head==NULL)
{
printf("您已经没有联系人可删了!\n");
return(head);
}
char MZ[20];
printf("请输入您要删除的联系人:");
scanf("%s",MZ);
struct book *p,*t;
t=head;
if(strcmp(head->name,MZ)==0)
{
head=t->next;
free(t);
printf("删除完成!");
return(head);
}
do
{
if(strcmp(t->name,MZ)<0&&strcmp((t->next)->name,MZ)==0)
{ p=t->next;
t->next=p->next;
free(p);
printf("删除完成!");
return(head);
}
t=t->next;
}while(t!=NULL);
if(t==NULL)
{printf("您是不是找错人啦!?\n");
return(head); }
}
void find(struct book *head)
{
struct book *p;
char MZ[20];
printf("请输入您要寻找的联系人:");
scanf("%s",MZ);
p=head;
if(p!=NULL)
do
{if(strcmp(p->name,MZ)==0)
{
printf("%s的号码为:%ld.\n",MZ,p->number);
break;
}
p=p->next;
}while(p!=NULL);
if(p==NULL)
printf("您的联系人走丢了!\n");
}
struct book*load(book *head)
{
struct book *p,*t;
FILE *fp;
if((fp=fopen("stu.list","rb"))==NULL)
{printf("读取失败!\n");
fclose(fp);
return(head);
}
for(p=(struct book*)malloc(LEN);;p=(struct book*)malloc(LEN))
{
if(fread(p,LEN,1,fp)==1)
{
if(head==NULL)
{
t=head=p;
}
else if(strcmp(p->name,head->name)<0)
{
p->next=head;
t=head=p;
}
else if (t->next!=NULL)
do
{if(strcmp(t->name,p->name)<0&&strcmp((t->next)->name,p->name)>0)
{
p->next=t->next;
t->next=p;break;
}
t=t->next;
}while(t->next!=NULL);
else if( t->next==NULL)
t->next=p;
}
if(feof(fp))
break;
}
printf("蹬蹬蹬等!加载成功!!\n");
fclose(fp);
return (head);
}
void save(struct book* head)
{
FILE*fp;
if((fp=fopen("stu.list","wb"))==NULL)
{
printf("无法打开文件!\n");
return;
}
for(;head!=NULL;head=head->next)
if(fwrite(head,LEN,1,fp)!=1)
{
printf("存储失败!\n");
fclose(fp);
}
fclose(fp);
}
int main()
{
struct book*head;
int i,t=1;
head=NULL;
head=load(head);
while(t==1)
{ printf("请按回车来到主界面");
fflush(stdin);//清空键盘缓冲区
getchar();
fflush(stdin);//清空键盘缓冲区
system("cls");//清屏
printf("╔═════════════════╗\n");
printf("║ 电话本管理程序 ║\n");
printf("╠═════════════════╣\n");
printf("║ 1 加入一个新联系人 ║\n");
printf("║ 2 删除一个联系人 ║\n");
printf("║ 3 查找电话本中的联系人 ║\n");
printf("║ 4 显示已存联系人 ║\n");
printf("║ 5 退 出 ║\n");
printf("╚═════════════════╝\n");
printf("请选择:");
scanf("%d",&i);
switch(i)
{
case 1:head=add(head);break;
case 2:head=del(head);break;
case 3:find(head);break;
case 4:print(head);break;
case 5:save(head);printf("感谢您的使用!\n");return 0;
}
}
}