11-12 #8

Merged
mac76tib2 merged 1 commits from gggg into master 2 years ago

@ -141,5 +141,56 @@ void returnBook(Reader* reader1, Book* book1)
} }
//C11 //C11
void save(Book* book1)
//C12 {
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;
}
Loading…
Cancel
Save