当前位置: 代码迷 >> C语言 >> [求助]我编了一个简单的图书管理程序,deletebook()不能正常工作?
  详细解决方案

[求助]我编了一个简单的图书管理程序,deletebook()不能正常工作?

热度:230   发布时间:2007-04-11 07:33:51.0
[求助]我编了一个简单的图书管理程序,deletebook()不能正常工作?

我编了一个图书管理程序,主要看我的deletebook()函数,我的意思是删除书就是将它置为空值,但我每次点删除就将我所有的纪录删了。不知道大家有没有好的法子?帮我顶一啦!
#include "stdio.h"
#include "conio.h"
#include "string.h"
struct Books /*定义一个图书信息结构体 Books*/
{
char bNum[10];
char bName[20];
char bPress[20];
char bWriter[20];
}book[1],*p,*q,*r; /*定义一个 book缓冲区*/

addbook() /*添加图书信息*/
{
FILE *fp;

char ch='y';
if ((fp=fopen("Books.txt","a+")) == NULL) /* 可追加形式打开文件 Books.txt*/
{
printf("Cannot open Books.txt!\n");
getch();
exit(-1);
}
while(ch=='y'||ch=='Y')
{
p=book; /*让指针p指向bInfo缓冲区*/
printf("input the book Number:(Than Press Enter)\n");
scanf("%s",p->bNum);
printf("input the book Name:\n");
scanf("%s",p->bName);
printf("input the book Press:\n");
scanf("%s",p->bPress);
printf("input the book Writer:\n");
scanf("%s",p->bWriter);
fwrite(p,sizeof(struct Books),1,fp); /*写入一本图书信息*/
printf("Press\n y :input next book\nanykey:back\n\n");
ch=getch();
}
fclose(fp);
}

int querybook() /*查询图书信息:以书名查询*/
{

FILE *fp;
char bookName[20];
int flag=0; /*flag为书存在的标志*/
if ((fp=fopen("Books.txt","r")) == NULL) /* 只读形式打开文件 Books.txt*/
{
printf("Cannot open Books.txt.\n");
getch();
exit(-1);
}
printf("Input the book Name you query:\n");
scanf("%s",bookName);
while(!feof(fp))
{
q=book; /*让指针q指向bInfo缓冲区*/
fread(q,sizeof(struct Books),1,fp);
if(strcmp(bookName,q->bName)==0) /*书名相同则输出*/
{
printf("BookNumber*BookName*BookPress*BookWriter\n");
printf("%-10s%-12s%-12s%-12s\n\n",q->bNum,q->bName,q->bPress,q->bWriter);
flag=1;
}
if(flag==1)
break;
}
if(flag==0)
printf("The book you queryed not exist!\n\n");
fclose(fp);
}

deletebook() /*删除图书信息*/
{
FILE *fp;
char bookName[20];
int flag=0; /*flag为书存在的标志*/
if ((fp=fopen("Books.txt","w+")) == NULL) /* open file Books.txt*/
{
printf("Cannot open Books.txt!\n");
getch();
exit(-1);
}
printf("Input the book you want to delete!\n");
scanf("%s",bookName);
while(!feof(fp))
{
r=book;
fread(r,sizeof(struct Books),1,fp);
if(strcmp(bookName,q->bName)==0)
{
strcpy(r->bNum, " "); /*找到书就令其所有项为空*/
strcpy(r->bName, " ");
strcpy(r->bPress, " ");
strcpy(r->bWriter, " ");
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n"); /*测试用*/
/*fseek(fp,-sizeof(struct Books),1); 将指针重新指向此书的头*/
fwrite(r,sizeof(struct Books),1,fp); /*写回*/
flag=1;
}
if(flag==1)
break;
}
if(flag==1)
printf("The book deleted!\n\n");
else
printf("The book not exist!\n\n");
fclose(fp);
}

main()
{
char ch;
printf("********************************************************************************\n");
printf(" Welcome to the books manage system\n");
printf("********************************************************************************\n");
printf("Press \n1: addbook \n2: querybook\n3: deletebook\n0: quit\n\n");
scanf("%c",&ch);
while(ch=='1'||ch=='2'||ch=='3'||ch=='0')
{

switch(ch)
{
case '1':addbook();
break;
case '2':querybook();
break;
case '3':deletebook();
break;
case '0':exit(1);
}
printf("Press \n1: addbook \n2: querybook\n3: deletebook\n0: quit\n\n");
ch=getch();
}
getch();
}

搜索更多相关的解决方案: 管理程序  deletebook  图书  

----------------解决方案--------------------------------------------------------
deletebook() /*删除图书信息*/
{
FILE *fp;
char bookName[20];
int flag=0; /*flag为书存在的标志*/
if ((fp=fopen("Books.txt","w+")) == NULL) /* open file Books.txt*/
{
printf("Cannot open Books.txt!\n");
getch();
exit(-1);
}
printf("Input the book you want to delete!\n");
scanf("%s",bookName);
while(!feof(fp))
{
r=book;
fread(r,sizeof(struct Books),1,fp);
if(strcmp(bookName,q->bName)==0) // q 应该改成 r 吧?
{
strcpy(r->bNum, " "); /*找到书就令其所有项为空*/
strcpy(r->bName, " ");
strcpy(r->bPress, " ");
strcpy(r->bWriter, " ");
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n"); /*测试用*/
/*fseek(fp,-sizeof(struct Books),1); 将指针重新指向此书的头*/
fwrite(r,sizeof(struct Books),1,fp); /*写回*/
flag=1;
}
if(flag==1)
break;
}
if(flag==1)
printf("The book deleted!\n\n");
else
printf("The book not exist!\n\n");
fclose(fp);
}
----------------解决方案--------------------------------------------------------

谢谢了!
好不容易有人理我。


----------------解决方案--------------------------------------------------------