diff --git a/图书管理系统.c b/图书管理系统.c index 46285b1..823a5ec 100644 --- a/图书管理系统.c +++ b/图书管理系统.c @@ -141,5 +141,56 @@ void returnBook(Reader* reader1, Book* book1) } //C11 - -//C12 \ No newline at end of file +void save(Book* book1) +{ + FILE* fp; + Book* pCur = book1; + int iCount = 0; + if (pCur == NULL) + { + printf("\n没有学生记录!\n"); + return; + } + if ((fp = fopen("book.txt", "wb")) == NULL) + { + printf("创建文件失败!\n"); + getchar(); + exit(1); + } + while (pCur) + { + fwrite(pCur, sizeof(Book), 1, fp); + pCur = pCur->next; + iCount++; + } + printf("\n"); + printf("保存文件的数据数目为:%d\n", iCount); + fclose(fp); +} +//C12 +Book* read1() +{ + FILE* fp; + Book* pHead = NULL, * pTemp = NULL, * pCur = NULL; + if ((fp = fopen("book.txt", "r")) == NULL) + { + printf("\n文件打开失败!请检查文件名!\n"); + exit(0); + } + pTemp = (Book*)malloc(sizeof(Book)); + while (fread(pTemp, sizeof(Book), 1, fp)) + { + if (!pHead) + { + pHead = pCur = pTemp; + } + else + { + pCur->next = pTemp; + pCur = pTemp; + } + pTemp = (Book*)malloc(sizeof(Book)); + } + fclose(fp); + return pHead; +} \ No newline at end of file