求教:C语言读取文件错误,附源代码 急用 麻烦了

来源:百度知道 编辑:UC知道 时间:2024/05/19 00:07:57
//保存图书资料
void savebook() //把图书信息写出文件的函数
{
FILE *fp;
struct book *p;
int i=0;

fp = fopen("totalbook.txt", "w");
if (fp == NULL)
{
printf("Can't open totalbook.txt\n");
return;
}
fprintf(fp,"%d", totalbook);
fclose(fp);

fp = fopen("book.txt", "w");
if (fp == NULL)
{
printf("Can't open book.txt\n");
return;
}

p = bookhead;

for(i = 0; i < totalbook; i++)
{
fwrite(p,sizeof(struct book),1,fp);
p = p->next;
}
fclose(fp);

printf("图书资料保存成功!\n");
}
//载入图书资料
struct book *loadbook() //从文件载入图书信息
{
FILE *fp;
struct book *p;
int i=0;

fp = fopen("totalbook.txt", "r");
if (fp == NULL)
{

#include <stdio.h>
#include <stdlib.h>

int totalbook = 0;
struct book{
char szBookName[1024];
book * next;
};
struct book *bookhead = NULL;

//保存图书资料
void savebook() //把图书信息写出文件的函数
{
FILE *fp;
struct book *p;
int i=0;

fp = fopen("new_totalbook.txt", "w");
if (fp == NULL)
{
printf("Can't open totalbook.txt\n");
return;
}
fprintf(fp,"%d", totalbook);
fclose(fp);

fp = fopen("new_book.txt", "w");
if (fp == NULL)
{
printf("Can't open book.txt\n");
return;
}

p = bookhead;

for(i = 0; i < totalbook; i++)
{
fprintf(fp,"%s\n",p->szBookName);
p = p->next;
if ( p == NULL )
{
break;
}
}
fclo