求修正!!谢谢了!!数据录入应该没问题了,输入两个以上的数据,在浏览时,始终从第二个图书数据开始出现乱码
void book_add(){
int i=0;
BOOK *p1=NULL,*p2=NULL;
BOOK *head=NULL;
FILE* fp;
for(;;)
{
A++;
i++;
if(head==NULL)
{
head=p1=p2=(BOOK*)malloc(sizeof(BOOK));
head->next=NULL;
}
else
{
p1=(BOOK*)malloc(sizeof(BOOK));
p1->next=NULL;
}
printf("编号:\n");
gets(p1->num);
printf("书名:\n");
gets(p1->name);
printf("作者:\n");
gets(p1->writer);
printf("分类号:\n");
gets(p1->fenlei);
printf("出版单位:\n");
gets(p1->pubdanwei);
printf("出版时间:\n");
gets(p1->pubtime);
p1->price=0.0f;
printf("价格:\n");
scanf("%f",&(p1->price));
getchar();
p2->next=p1;
p2=p1;
if((fp=fopen("Book.txt","ab+"))==NULL)
{
fp=fopen("Book.txt","wb+");
}
fwrite(p1,sizeof(BOOK),1,fp);
fclose(fp);
break;
}
}
void book_view()
{
int i=0;
FILE* fp;
BOOK *p1=NULL;
BOOK *p2=NULL;
fp=fopen("Book.txt","rb+");
if(fp==NULL)
{
printf("打开失败,尚无图书信息\n");
return;
}
else
{
for(i=0;i<A;i++)
{
p1=(BOOK*)malloc(sizeof(BOOK));
fread(p1,sizeof(BOOK),1,fp);
p2=p1;
p2->next=NULL;
printf("编号:%s\n",p1->num);
printf("书名:%s\n",p1->name);
printf("作者:%s\n",p1->writer);
printf("分类号:%s\n",p1->fenlei);
printf("出版单位:%s\n",p1->pubdanwei);
printf("出版时间:%s\n",p1->pubtime);
printf("价格:%f\n",p1->price);fclose(fp);
p1->next=p2;
p2=p1;
}
fclose(fp);
}
} A已经在外部定义为了一个计数器