各位,你们看看为什么fwrite函数只能输出一组数据,程序能运行
#include<stdio.h>#include<stdlib.h>
#include<string.h>
typedef struct bk {
char book_num[10];
char book_name[10];
int book_kc;
int book_xc;
struct bk * next;
}book;
struct bk *l;
void save();
void menu();
void putin();
void search() ;
void lend();
void reback();
void save(char book_num[],char book_name[],int num)
{ FILE *book_fp;
struct bk *t;
if((book_fp=fopen("C:\\Users\\zll\\Downloads\\book.txt","wb"))==NULL)
{ printf("不能建立图书文件!\n"); return; }
t=(struct bk *)malloc(sizeof(struct bk));
strcpy(t->book_num,book_num);
strcpy(t->book_name, book_name);
t->book_kc=t->book_xc=num;
l->next=t;t->next=NULL;
fwrite(t,sizeof(struct bk),1,book_fp);
}
void putin()
{
struct bk *p;
p=(struct bk *)malloc(sizeof(struct bk));
int number;
int y=1;
system("cls");
while(y)
{
printf("请输入您的书的编号:");
scanf("%s",p->book_num);
printf("请输入书名:");
scanf("%s",p->book_name);
printf("请输入您要存的数量:");
scanf("%d",&number);
save(p->book_num,p->book_name,number);
printf("\n是否继续录入(1.继续;0,退出录入):");
scanf("%d",&y);
switch(y)
{
case 1:putin();
break;
case 0:system("cls");menu();
break;
}
}
}
void search()
{ struct bk *p;
p=(struct bk *)malloc(sizeof(struct bk));
printf("请输入您的书的编号:");
scanf("%s",p->book_num);
while(p!=NULL&&strcmp(p->book_num,l->book_num)!=0)
{ l=l->next;
if(l==NULL) printf("\n\t此编号对应的图书不存在!\n");
}
printf("\n\t图书编号\t图书名\t库存量\t现存量 \n");
printf(" %10s\t %10s\t %d\t %d\t\n",l->book_num,l->book_name,l->book_kc,l->book_xc);
menu();
}
void lend()
{ struct bk *q;
q=(struct bk *)malloc(sizeof(struct bk));
printf("请输入您的书的编号:");
scanf("%s",q->book_num);
while(q!=NULL&&strcmp(q->book_num,l->book_num)!=0)
{
l=l->next;
if(l==NULL)
printf("\n\t此编号对应的图书不存在!\n");
}
printf("\n\t图书编号\t图书名\t库存量\t现存量 \n");
printf(" %10s\t %10s\t %d\t %d\t\n",l->book_num,l->book_name,l->book_kc,l->book_xc);
l->book_xc=l->book_xc-1;
save(l->book_num,l->book_name,l->book_xc);
menu();
}
void reback()
{
struct bk *b;
b=(struct bk *)malloc(sizeof(struct bk));
printf("请输入您的书的编号:");
scanf("%s",b->book_num);
while(b!=NULL&&strcmp(b->book_num,l->book_num)!=0)
{
l=l->next;
if(l==NULL)
printf("\n\t此编号对应的图书不存在!\n");
}
l->book_xc=l->book_xc+1;
save(b->book_num,b->book_name,b->book_xc);
printf("\n\t图书编号\t图书名\t库存量\t现存量 \n");
printf(" %10s\t %10s\t %d\t %d\t\n",l->book_num,l->book_name,l->book_kc,l->book_xc);
printf("\n您的书几经归还,谢谢!") ;
menu();
}
void menu()
{
int lm;
printf(" \n ***************************************************** ");
printf(" \n ★图书管理系统★ ");
printf(" \n ㈠图书入库 ");
printf (" \n ㈡查询图书 ");
printf(" \n ㈢图书借阅 ");
printf(" \n ㈣图书归还 ");
printf(" \n ㈤退出系统 ");
printf(" \n 按上述1/2/3/4/5数字键选择以上功能 ");
printf(" \n ******************************************************* ");
scanf("%d",&lm);
switch(lm)
{
case 1:putin();
break;
case 2:search();
break;
case 3:lend();
break;
case 4:reback();
case 5:break;
}
}
main()
{
l=(struct bk *)malloc(sizeof(struct bk));
menu();
return 0;
}