图书管理系统,文件写入,读出的问题
图书管理系统在vs上第一次运行可以,但存入数据后,第二次运行就出现断点,
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//书籍信息
typedef struct BookInfo
{
char name[20];
float price;
int num;
}Info;
//书籍节点
typedef struct BookNode
{
Info date;
struct BookNode* next;//链表
}Node;
//创建表头
Node* creatHeadNode(void) //不做数据存储
{
Node* HeadNode = (Node*)malloc(sizeof(Node));
HeadNode->next = NULL;
return HeadNode;
}
//创建节点
Node* creatNode(Info news)
{
Node* NewNode = (Node*)malloc(sizeof(Node));
NewNode->date = news;
NewNode->next = NULL;
return NewNode;
}
Node* List = NULL;//全局变量,表头
//打印书籍目录
void printList(Node* HeadNode)
{
Node* tempNode = HeadNode->next;
printf("书名\t\t价格\t\t数量\n");
while (tempNode)
{
printf("%-12s %-12.1f %-12d\n", tempNode->date.name, tempNode->date.price, tempNode->date.num);
tempNode = tempNode->next;
}
}
//添加书籍
void AddnewBook(Info BookInfo, Node* HeadNode)
{
Node* newNode = creatNode(BookInfo);
newNode->next = HeadNode->next;
HeadNode->next = newNode;
}
//删除书籍
void DeleteBook(Info deletebook, Node* HeadNode)
{
Node* flont = HeadNode;
Node* back = HeadNode->next;
while (back && strcmp(deletebook.name, back->date.name) != 0)
{
flont = back;
back = back->next;
}
if (back == NULL)
{
printf("图书系统无此书!\n");
}
else
{
flont->next = back->next;
free(back);
back = NULL;
}
}
//写入文件(写操作)
void writeBookInfoTofile(Node* List)
{
FILE* Pbook;
fopen_s(&Pbook,"Book_store.txt", "w") ;
Node* SecondNode = List->next;
while (SecondNode)
{
fprintf(Pbook, "%12s %12.1f %12d\n", SecondNode->date.name, SecondNode->date.price, SecondNode->date.num);
SecondNode = SecondNode->next;
}
fclose(Pbook);
}
//读取文件(读操作)
void ReadBookInfoTofile(Node* List)
{
FILE* Pbook;
Info tempInfo;
fopen_s(&Pbook, "Book_store.txt", "r");
if (Pbook == NULL) //第一次,使用系统,文件一定不存在,创建一个
{
fopen_s(&Pbook, "Book_store.txt", "r");
}
while (fscanf_s(Pbook,"%12s %12.1f %d\n", tempInfo.name,20,&tempInfo.price,sizeof(float),&tempInfo.num,sizeof(int))!=EOF)
{
AddnewBook(tempInfo, List);
}
fclose(Pbook);
}
//图书操作界面
void BookFace_group(void)
{
printf(" \tXXX图书管理系统 \n");
printf("*********************************\n");
printf("*\t0->退出系统 *\n");
printf("*\t1->查找书籍 *\n");
printf("*\t2->删除书籍 *\n");
printf("*\t3->借阅书籍 *\n");
printf("*\t4->归还书籍 *\n");
printf("*\t5->排序书籍 *\n");
printf("*\t6->书籍清单 *\n");
printf("*\t7->添加书籍 *\n");
printf("*********************************\n");
}
//用户操作按键处理
void UserBykey(void)
{
int key;
Node* tempNode;
Info tempInfo;
scanf_s("%d", &key);
switch (key)
{
case 0:
printf("退出系统!\n");
exit(0);
case 1:
printf("查找书籍\n");
break;
case 2:
printf("删除书籍\n");
scanf_s("%s", tempInfo.name, 20);
DeleteBook(tempInfo, List);
writeBookInfoTofile(List);
printf("删除成功!\n");
break;
case 3:
printf("借阅书籍\n");
break;
case 4:
printf("归还书籍\n");
break;
case 5:
printf("排序书籍\n");
break;
case 6:
printf("书籍清单\n");
printList(List);
break;
case 7:
printf("添加书籍\n");
scanf_s("%s %f %d", tempInfo.name, 20, &tempInfo.price, &tempInfo.num);
AddnewBook(tempInfo, List);
writeBookInfoTofile(List);
printf("添加成功!\n");
break;
default:
printf("非法输入,请重新输入!\n");
UserBykey();
break;
}
}
int main()
{
List = creatHeadNode();
ReadBookInfoTofile(List);
while (1)
{
BookFace_group();
UserBykey();
system("pause");
system("cls");
}
return 0;
}
如照片所示问题,要怎么解决?