Merge pull request '11-12' (#8) from gggg into master

c1
mac76tib2 2 years ago
commit f59ac84804

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