C++ 图书馆系统 借阅函数有问题,无论怎么输都是图书已借完
void borrow_bookname(){
FILE *fp,*fp3;
struct book *head=NULL;
struct book *p,*p1,*p2;
int tNo=0,tnum=0,i,loop,num=0,n=0,k=0,t=0,flag=0;
char tname[20]={'\0'},tauthor[20]={'\0'},ttname[20]={'\0'},name[20]={'\0'},zname[20],day[20]={'\0'},bookname[20]={'\0'};
char hitkey=0;
system ("cls");
printf(" ————————欢迎使用图书管理系统————————\n");
puts("");
printf("\n* * * 借书界面 * * *\n");
puts("");
if ((fp=fopen(FNAME,"r"))==NULL)
{
system ("cls");
printf("图书馆无库存!按任意键退出!");
getch();
exit(0);
}
else
{
{
printf("请输入借阅书名:");
scanf("%s",zname);
k= tjbook();
for (i=0;i<k;i++)
{
fscanf(fp,"%d%s%s%d",&tNo,tname,tauthor,&tnum);
n++;
if (n==1)
{
p1=p2=(struct book*)malloc(LEN);
head=p1;
}
else
{
p2->next=p1;
p2=p1;
p1=(struct book*)malloc(LEN);
}
p1->No=tNo;
strcpy(p1->name,tname);
strcpy(p1->author,tauthor);
p1->num=tnum;
}
if (n==0)
head=NULL;
else
{
p2->next=p1;
p1->next=NULL;
fclose(fp);
}
}
}
p=head;
for (;p!=NULL;)
{
if(!(strcmp(p->name,zname)))
{
flag=1;
loop=p->num;
(p->num)--;
}
p=p->next;
}
if(flag&&loop==1)
{
fp=fopen(FNAME,"w");
fclose(fp);
fp=fopen(FNAME,"a");
p=head;
for (;p!=NULL;)
{
fprintf(fp,"%d %s %s %d ",p->No,p->name,p->author,p->num);
p=p->next;
}
free(p);
fclose(fp);
}
if(flag&&loop==1)
{
if((fp3=fopen(fname,"r"))==NULL)
{
fp3=fopen(fname,"w");
fclose(fp3);
}
fclose(fp);
fclose(fp3);printf("成功!按任意键返回\n");
getch();
system("cls");
reader();
}
reader();
}
else
printf("此书已被借完!按任意键返回!");
getch();
system("cls");
reader();