C链表和文件操作实战--图书管理系统
C链表和文件操作实战--图书管理系统
这个貌似是我们大一的C语言课程设计,当时是从网上下的代码,然后修改了一下.现在想想好惭愧,哎我好想重新开始,以前自己的确失去了好多东西,希望今后的自己好好......
时间过的真快,现在都大三了,马上也要滚蛋了,再滚蛋之前好好修炼内功.于是我又重新拾起了久违的C语言,用了几天时间认真看完了Programming in c,个人觉得这本书讲得真好,
收益亮多.回想起大一学的C语言,我们用的教材是学校自己编的,往事不堪回首啊....记得上学期我们学习数字信号处理,我么用的也是学校自己编的书,当时上课最有特色的地方就是:我们那个老师每节课都要花好长时间纠正书里的错误,他经常说的一句话是-还好当时我没参加编写这本书,不然还不被骂死.......
我始终想不明白的是有那么多经典的好书,学校不用,非要自己去编那么操蛋的书.这学期我们学习XML应用教材,当时老师说本来想用那本书的,觉得很好.最后学校不同意,理由竟是:那本是高职的书,我们是本科....我听完泪流满面啊.我们的大学,我们的老师,你们什么时候才能不浮躁???
哎,本来我不想说什么,但已经到这个地步了,就再多废话几句吧.我们学了微机原理,老师没在课上编过.调试过一个程序,那么经典的debug工具都不提...;我们学了计算机网络,天天讲各种包,老师从没在课上用抓包软件抓过一个包,分析过一个包...;我们学了组网技术,老师给我的模拟器不知道是哪一年的,ospf都不支持,天天说cisco,没介绍过packettracer, dynamips...泪流满面啊,我们的学校很差,但真的差到这个地步了吗? 老师们,你们每天都在想什么了,能不浮躁吗? 天天说上课学生不听讲,天天说上课人数少,为什么呀?我们学生是有问题,但你们呢? 你们真的对得起你们拿的工资吗?
哎,话多了.一个学生的牢骚而已....
stop,不说了,呵呵.
主要是想起了大一做的课程设计,所以自己动手做了下,不过我这个事简化版,呵呵,温故而知新.
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <conio.h>
- #define books "f:\\books.txt"
- #define booksbak "f:\\booksbak.txt"
- struct bookinfo
- {
- char isbn[20];
- char title[30];
- char author[20];
- int count;
- };
- struct book
- {
- struct bookinfo onebook;
- struct book *next;
- };
- struct book *searchBook ( struct book *listptr,char isbn[])
- {
- while(listptr!=(struct book *)0)
- if (strcmp(listptr->onebook.isbn,isbn)==0)
- return listptr;
- else
- listptr=listptr->next;
- return (struct book *)0;
- }
- void MainSearchbook(struct book *firstptr)
- {
- struct book *ptr;
- char isbnno[20];
- printf("请输入ISBN:");
- scanf("%s",&isbnno);
- ptr=searchBook(firstptr,isbnno);
- if (ptr!=(struct book *)0)
- {
- printf("找到了!!!\n");
- printf("ISBN:%s\n",ptr->onebook.isbn);
- printf("Title:%s\n",ptr->onebook.title);
- printf("Author:%s\n",ptr->onebook.author);
- }
- else
- printf("sorry,not found!!!\n");
- }
- int addBook(struct book *listptr,struct bookinfo note)
- {
- while(listptr->next!=0)
- listptr=listptr->next;
- listptr->next=(struct book *)malloc(sizeof(struct book));
- listptr->next->onebook=note;
- listptr->next->next=0;
- return 0;
- }
- void MainAdd(struct book *listptr,FILE *fp)
- {
- int ok;
- struct bookinfo note;
- printf("请输入ISBN:");
- scanf("%s",¬e.isbn);
- printf("请输入Title:");
- scanf("%s",¬e.title);
- printf("请输入Author:");
- scanf("%s",¬e.author);
- ok=addBook(listptr,note);
- if (ok==0)
- {
- //将加入的图书写到文件中保存
- fprintf(fp,"\n%s %s %s %d",note.isbn,note.title,note.author,0);
- printf("添加图书成功!!!\n");
- }
- else
- printf("添加图书失败!!!\n");
- }
- int removeBook(struct book *listptr,char isbn[])
- {
- while(listptr->next!=(struct book *)0)
- {
- if (strcmp(listptr->next->onebook.isbn,isbn)==0)
- {
- listptr->next=listptr->next->next;
- return 0;
- }
- else
- listptr=listptr->next;
- }
- return -1;
- }
- void MainRemove(struct book *listptr,FILE *fp)
- /************************************************************************/
- /* 删除书籍函数,通过ISBN删除链表节点,同时删除文件中对应信息 */
- /* 删除文件中一行,用的是笨方法,把需要的信息写到新文件,删除旧文件,重命名..*/
- /************************************************************************/
- {
- char isbnno[20];
- int ok;
- struct bookinfo onebook;
- printf("请输入ISBN:");
- scanf("%s",&isbnno);
- ok=removeBook(listptr,isbnno);
- if (!ok)
- {
- FILE *fpbak;
- if ((fpbak=fopen(booksbak,"a+"))==NULL)
- printf("文件打开失败!!!\n");
- fseek(fp,0,SEEK_SET); //移到文件开始
- while((fscanf(fp,"%s %s %s %d\n",&onebook.isbn,&onebook.title,&onebook.author,&onebook.count))!=EOF)
- {
- if (strcmp(onebook.isbn,isbnno)!=0)
- {
- fprintf(fpbak,"%s %s %s %d\n",onebook.isbn,onebook.title,onebook.author,onebook.count);
- }
- }
- fclose(fp);
- fclose(fpbak);
- if (remove(books)) //删除失败返回非0
- {
- printf("删除文件失败!!!\n");
- return ;
- }
- else
- if (rename(booksbak,books)) //重命名失败返回非0值
- {
- printf("重命名失败!!!\n");
- return ;
- }
- printf("删除成功!!!\n");
- }
- else
- printf("查无此书!!!");
- }
- int choice(void)
- {
- int c;
- printf("1.查看图书\n");
- printf("2.添加图书\n");
- printf("3.删除图书\n");
- printf("4.退出程序\n");
- printf("请选择序号:");
- return c=getchar();
- //return c=getche();
- printf("\n\n");
- }
- int addEntry(FILE *fp,struct book *firstptr)
- /************************************************************************/
- /* 主要用来加载文件中存放的图书信息 */
- /************************************************************************/
- {
- struct bookinfo onebook;
- while((fscanf(fp,"%s %s %s %d\n",&onebook.isbn,&onebook.title,&onebook.author,&onebook.count))!=EOF)
- {
- while(firstptr->next!=0)
- firstptr=firstptr->next;
- firstptr->next=(struct book *)malloc(sizeof(struct book));
- firstptr->next->onebook=onebook;
- firstptr->next->next=0;
- }
- return 0;
- }
- int main(int argc,char *argv[])
- {
- int ch;
- struct book first;
- strcpy(first.onebook.isbn,"123456");
- strcpy(first.onebook.title,"Programming C");
- strcpy(first.onebook.author,"yhb");
- first.next=0;
- struct book *firstptr=&first; //链表头指针
- FILE *fp;
- if ((fp=fopen(books,"a+"))==NULL)
- printf("文件打开失败!!!");
- addEntry(fp,firstptr);
- while(1)
- {
- system("CLS"); //清屏
- /************************************************************************/
- /* 想想这里为什么要清空缓冲区? */
- /*由于上一次(choice函数)的getchar(),还有一个'\n'留在缓冲区.... */
- /*可以把这句话注释掉看看,没有这句话会遇到麻烦 */
- /*如果不用fflush,可以将上面的getchar()换成getche() */
- /*比较getchar(),getch(),getche()...... */
- /************************************************************************/
- fflush(stdin);
- ch=choice()-48;
- switch (ch)
- {
- case 1:
- MainSearchbook(firstptr);
- break;
- case 2:
- MainAdd(firstptr,fp);
- break;
- case 3:
- MainRemove(firstptr,fp);
- break;
- case 4:
- printf("谢谢使用...\n");
- exit(0);
- default:
- printf("请输入正确序号!");
- }
- system("PAUSE");
- }
- return 0;
- }
bookinfo结构体的count成员,本来是想做最近热门图书的, 通过用户查看书籍,统计书籍浏览的次数.发现更新文件中的数据很麻烦,于是就取消了.呵呵
books.txt里面的数据大致如下所示:可以直接通过编辑books.txt增加书籍.
- 123 python yhb 2
- 456 linux lwy 4
- 789 c yl 5
文件操作用fread,fwrite应该更方便点.
评论暂时关闭