好吧,我有我读二进制文件代码的问题...二进制文件写入/读取的问题
首先,我会告诉你我写代码:
void book_saving(char *file_name, struct BOOK *current)
{
FILE *out;
BOOK buf;
out = fopen(file_name, "wb");
if(out != NULL)
{
printf_s("Writting to file...");
do
{
if(current != NULL)
{
strcpy(buf.catalog_number, current->catalog_number);
strcpy(buf.author, current->author);
buf.price = current->price;
strcpy(buf.publisher, current->publisher);
strcpy(buf.title, current->title);
buf.price = current->year_published;
fwrite(&buf, sizeof(BOOK), 1, out);
}
current = current->next;
} while(current != NULL);
printf_s("Done!\n");
fclose(out);
}
}
,这里是我的“版本”阅读:
int book_open(struct BOOK *current, char *file_name)
{
FILE *in;
BOOK buf;
BOOK *vnext;
int count;
int i;
in = fopen("west", "rb");
printf_s("Reading database from %s...", file_name);
if(!in)
{
printf_s("\nERROR!");
return 1;
}
i = fread(&buf,sizeof(BOOK), 1, in);
while(!feof(in))
{
if(current != NULL)
{
current = malloc(sizeof(BOOK));
current->next = NULL;
}
strcpy(current->catalog_number, buf.catalog_number);
strcpy(current->title, buf.title);
strcpy(current->publisher, buf.publisher);
current->price = buf.price;
current->year_published = buf.year_published;
fread(&buf, 1, sizeof(BOOK), in);
while(current->next != NULL)
current = current->next;
fclose(in);
}
printf_s("Done!");
return 0;
}
我只需要保存我的链接列表在二进制文件,并能够阅读它...请帮助我。该计划只是不看它或它的崩溃每一次不同的情况......
向我们展示struct BOOK的定义。 – 2010-05-16 22:51:38
我假设'BOOK'有'char []'而不是'char *'声明... – Phil 2010-05-16 22:57:56