输入数据->文件存储->退出程序->重新运行->文件读取->数据统计->全部显示,之后发现之前存储的数据都没有了,求大神们帮忙看一下问题在哪
void save(const BOOK *top) //文件存储
{
const BOOK *q;
FILE *fp;
if(count==0)
{
printf("\n当前没有记录可存!");
return;
}
if((fp=fopen("D://bookmanage1.txt","wb"))==NULL)
{
printf("\n不能打开文件!\n");
exit(1);
}
printf("\n文件正在存储中……\n");
q=top->next;
while(q)
{
printf("a");
fwrite(q,LEN,1,fp);
printf("b");
q=q->next;
printf("c");
}
fclose(fp);
printf("%d条记录已经存入文件,请继续操作。\n",count);
}
BOOK *load(BOOK *top) //文件读取
{
FILE *fp;
BOOK *q,*old;
count=0;
if((fp=fopen("D://bookmanage1.txt","rb"))==NULL)
{
printf("打不开文件!");
exit(1);
}
printf("\t正在读取文件中……\n");
ASK(q);
top=q;
old=top;
while(!feof(fp))
{
if(1!=fread(q,LEN,1,fp)) break;
count=count+1;
ASK(q->next);
//old=q->next;
old=q;
q=q->next;
}
old->next=NULL;
fclose(fp);
printf("\t取出%d条记录。\n",count);
return(top);
}
int estatistical(BOOK top) //统计数据
{
BOOK *p=top;
char choose[100],input[100];
int cn=0;
int flag=0;
printf("\t***********************************************************\n");
printf("\t**\t\t请选择按照哪种方式进行统计\t \n");
printf("\t [1] 书名 [2] 主编\t \n");
printf("\t [3] 出版社 [4] 出版年\t \n");
printf("\t [5] ISBN书号 [6] 全部显示\t \n");
printf("\t**********************************************************\n");
gets(choose);
switch(cn)
{case 6:
display(p);
}
}
void display(const BOOK *top) //显示记录
{
const BOOK *q;
if(count==0)
{
printf("\n现在还没有记录!\n");
return;
}
q=top->next;
printf("书籍信息如下:\n");
printf("书名 主编 出版社 出版年 版次 ISBN书号 定价 拥有本数 现余本数 借出本数\n");
while(q!=NULL)
{
printf("%s\t%s\t%s\t%d\t%s\t%s\t%.2f\t%d\t%d\t%d\n",q->name,q->editor,q->press,q->pressyear,q->vision,q->ISBN,q->price,q->own,q->left,q->borrow);
q=q->next;
}
}
BOOK *badd(BOOK *top) //添加数据
{
BOOK *old;
BOOK *p,*q;
FILE *fp;
int flag;
ASK(p);
old=top;
while(old->next!=NULL)
{
old=old->next;
}
printf("输入数据,输入‘0’时结束。\n");
--count;
do
{
++count;
printf("\n书名:");
scanf("%s",&p->name);
if(strcmp(p->name,"0")==0)
break;
printf("\n主编:");
scanf("%s",&p->editor);
printf("\n出版社:");
scanf("%s",&p->press);
printf("\n出版年:");
scanf("%d",&p->pressyear);
printf("\n版次:");
scanf("%s",&p->vision);
printf("\nISBN书号:");
scanf("%s",&p->ISBN);
printf("\n定价:");
scanf("%lf",&p->price);
printf("\n现余本数:");
scanf("%d",&p->left);
printf("\n借出本数:");
scanf("%d",&p->borrow);
p->own=p->borrow+p->left;
p->next=NULL;
if(count==0) top=p;
old->next=p;
old=p;
ASK(p);
}while(p->name[0]!='0');
old->next=NULL;
free(p);
printf("输入成功!\n");
return(top);
}