下面是一段自定义函数,但老不出错误,帮忙改改吧
void BookAdd()
{
FILE *fp;
Book consultbook;
fp=fopen("book.txt","rb");
if(fp==NULL)
fp=fopen("book.txt","wb");
else
{
fclose(fp);
fp=fopen("book.txt","ab");
}
clrscr();/*清屏*/
printf("Intput new book num: ");
gets(consultbook.num);
gotoxy(2,4);
printf("Input new book name: ");
gets(consultbook.name);
gotoxy(2,5);
printf("Please input new book's price: ");
scanf("%d%*c",&consultbook.price);
strcpy(consultbook.person,"");
consultbook.yes=1;
consultbook.next=NULL;
fwrite(&consultbook,sizeof(Book),1,fp);
fclose(fp);
clrscr();
}
void BookConsult()
{
FILE *fp;
Book consultbook;
char bookname[20];
int flag=0,i=0;
ClrScr();
gotoxy(2,3);
printf("Input the book name: ");
gets(bookname);
fp=fopen("book.txt","rb");
if(fp==NULL)
{
clrscr();
gotoxy(2,3);
printf("Error!");
fclose(fp);
return;
}
else
while(!feof(fp))
{
fread(&consultbook,sizeof(Book),1,fp);
if(strcmp(consultbook.name,bookname)==0)/*如果查找到*/
{
clrscr();
gotoxy(2,3+i*5);
printf("This book %s information as follow:",consultbook.name);
gotoxy(2,4+i*5);
printf("This book num is %s",consultbook.num);
gotoxy(2,5+i*5);
printf("This book's price is %d",consultbook.price);
gotoxy(2,6+i*5);
printf("This book's is browwed by %s",consultbook.person);
gotoxy(2,7+i*5);
printf("This book's state is %d",consultbook.yes);
i++;
flag=1;
}
}
if(flag==0)
{
clrscr();
gotoxy(2,3);
printf("No found this book");
}
fclose(fp);
}
void BookDel()
{
FILE *fp;
Book *head,*p,*q;
char bookname[20];
fp=fopen("book.txt","rb");
if(fp==NULL)
{
gotoxy(2,3);
printf("Error!");
fclose(fp);
return;
}
head=p=q=(Book*)malloc(sizeof(Book));
fread(p,sizeof(Book),1,fp);
while(!feof(fp))
{
q=p;
p=(Book*)malloc(sizeof(Book));
fread(p,sizeof(Book),1,fp);
q->next=p;
}
p->next=NULL;
fclose(fp);
clrscr();
printf("Input the book name: ");
gets(bookname);
p=head;
while(p!=NULL)
{
if(strcmp(p->name,bookname)==0)
{
if(p==head)
head=head->next;
else
q->next=p->next;
break;
}
q=p; /*指针后移*/
p=p->next;
}
fp=fopen("book.txt","wb");
while(head!=NULL)
{
fwrite(head,sizeof(Book),1,fp);
head=head->next;
}
fclose(fp);
clrscr();
}
void BookBorrow()
{
FILE *fp;
Book consultbook;
Member consultmember;
char bookname[20],membername[20];
int flag=0,mflag=0;
clrscr();
gotoxy(2,3);
printf("Input the mermber name: ");
gets(membername);
if((fp=fopen("member.txt","rb"))==NULL)
{
gotoxy(3,3);
printf("Cannot open file\n!");
return;
}
else
while(!feof(fp))
{
fread(&consultmember,sizeof(Member),1,fp);
if(strcmp(consultmember.name,membername)==0)
{
mflag=1;
break;
}
}
fclose(fp);
if(mflag==0)
{
gotoxy(2,4);
printf("You not is a member,pleas register!");
return;
}
gotoxy(3,5);
printf("Input the book name: ");
gets(bookname);
clrscr();
if((fp=fopen("book.txt","rb+"))==NULL)
{
gotoxy(2,3);
printf("Cannot open file\n!");
return;
}
while(!feof(fp))
{
fread(&consultbook,sizeof(Book),1,fp);
if(strcmp(consultbook.name,bookname)==0)
{
if(consultbook.yes==0)
{
gotoxy(2,3);
printf("This book has borrowed\n!");
break;
}
else
{
consultbook.yes=0;
strcpy(consultbook.person,membername);
fseek(fp,-1L*sizeof(Book),1);
fwrite(&consultbook,sizeof(Book),1,fp);
gotoxy(2,3);
printf("Borrowed succ!");
flag=1;
break;
}
}
}
if(flag!=1)
{
clrscr();
gotoxy(2,3);
printf("Borrowed fail!\n");
}
fclose(fp);
}
void BookReturn()
{
FILE *fp;
Book consultbook;
char bookname[20];
int flag=0;
clrscr();
if((fp=fopen("book.txt","rb+"))==NULL)
{
gotoxy(2,3);
printf("cannot open file\n!");
return;
}
printf("Input the book name: ");
gets(bookname);
clrscr();
while(!feof(fp))
{
fread(&consultbook,sizeof(Book),1,fp);
if(strcmp(consultbook.name,bookname)==0)
{
if(consultbook.yes==0)
{
consultbook.yes=1;
strcpy(consultbook.person,"");
fseek(fp,-1L*sizeof(Book),1);
fwrite(&consultbook,sizeof(Book),1,fp);
gotoxy(2,3);
printf("Return book succ!");
flag=1;
break;
}
}
}
if(flag!=1)
printf("Return fail!\n");
fclose(fp);
}