| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 583 人关注过本帖
标题:[求助]我编了一个简单的图书管理程序,deletebook()不能正常工作?
只看楼主 加入收藏
oucluo
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-10-20
收藏
 问题点数:0 回复次数:2 
[求助]我编了一个简单的图书管理程序,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 图书 
2007-04-11 07:33
Javal
Rank: 1
等 级:新手上路
威 望:1
帖 子:108
专家分:0
注 册:2006-5-7
收藏
得分:0 
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);
}

猝然临之而不惊,无故加之而不怒 /?spaced" target="_blank">Linux C资料
2007-04-11 14:42
oucluo
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-10-20
收藏
得分:0 

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

2007-04-13 12:10
快速回复:[求助]我编了一个简单的图书管理程序,deletebook()不能正常工作?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033921 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved