C 二进制文件写入/读取问题
好的,我读二进制文件的代码有问题 首先,我将向您展示我的书写代码:C 二进制文件写入/读取问题,c,file,binary,C,File,Binary,好的,我读二进制文件的代码有问题 首先,我将向您展示我的书写代码: 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 {
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;
}
我只需要将我的链表保存在二进制文件中,并能够读回。。。请帮帮我。程序只是不读它或每次不同的情况下崩溃
do..while
循环可以形成得更好。如果你要在结尾检查,不要在开头检查。如果您发现必须这样做,则可能没有使用正确的流量控制。例如,这里您应该只说while(current!=NULL){}
做什么?您正在将循环中的当前节点设置为全新的书籍
,并使其下一个元素为空
。为什么?为什么不在书写方法中镜像你的循环呢
current==NULL
隐式地看一下你在做什么-你的阅读方法是strcpy
ing。不要那样做fclose(in)
在中,而在打开的书中循环
好的,我编辑了代码,做了两个假设
BOOK
只有一个指针(next
),其他所有内容都是一个分配了内存的数组FILE *out;
BOOK buf;
out = fopen(file_name, "wb");
if(out == NULL) return;
printf_s("Writing to file...");
while(current != NULL)
{
fwrite(&buf, sizeof(BOOK), 1, out);
current = current->next;
}
printf_s("Done!\n");
fclose(out);
book\u open-获取指向book
int book_open(struct BOOK **current, char *file_name)
{
FILE *in;
BOOK *buf; // a pointer with malloc'd memory - can't reuse the local variable version!
BOOK *vnext = *current;
int i;
in = fopen("west", "rb"); // I hope that's the name of your file
printf_s("Reading database from %s...", file_name);
if(!in)
{
printf_s("\nERROR!");
return 1;
}
while(1)
{
buf = malloc(sizeof(BOOK));
i = fread(&buf,sizeof(BOOK), 1, in);
if(feof(in))
{
free(buf); // never made it in
break;
}
buf->next = NULL; // the 'next' written to file is certainly not the same
// point current to it if empty, else point to next
if(*current == NULL) *current = buf;
else
{
wnext->next = buf;
wnext = buf; // next iteration you'll be setting buf->next
}
}
fclose(in);
printf_s("Done!");
return 0;
}
我认为这样更好。看起来您可能正在尝试传入一个已填充的现有列表,或者如果一个列表未传入,则read函数将尝试分配并创建一个列表。两种情况看起来都不太对
如果它是第一个(传入现有列表),那么
while(current->next!=NULL)
循环将扫描到它的末尾。如果您试图创建一个新列表,那么看起来需要做一些额外的工作来将新节点链接在一起 向我们展示struct BOOK的定义。我假设BOOK
声明了char[]
而不是char*
。。。