2010-05-16 114 views
0

好吧,我有我读二进制文件代码的问题...二进制文件写入/读取的问题

首先,我会告诉你我写代码:

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; 
} 

我只需要保存我的链接列表在二进制文件,并能够阅读它...请帮助我。该计划只是不看它或它的崩溃每一次不同的情况......

+1

向我们展示struct BOOK的定义。 – 2010-05-16 22:51:38

+0

我假设'BOOK'有'char []'而不是'char *'声明... – Phil 2010-05-16 22:57:56

回答

2
  1. do..while圈可以形成更好。如果你最后要检查,不要在开始时检查。如果你发现你必须这样做,你可能没有使用正确的流量控制。例如,在这里你应该只是说while(current != NULL) { }

  2. 你想用if(current != NULL) { }做什么?您正在将循环中的当前节点设置为全新的BOOK,并使其下一个元素NULL。为什么?为什么不直接反映你在写作方法中的循环?

  3. 看看你在做什么,如果current == NULL含蓄 - 你是strcpy在你的阅读方法。不要这样做。

  4. 您似乎在book_openwhile循环内说fclose(in)

一旦我编译它,我会得到更多。


好的,我编辑的代码稍微使2个假设

  1. 这不是一个问题功课
  2. BOOK仅具有1个指针(next)和其他一切是与存储器阵列分配它

book_saving - 简单循环和写入

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_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; 
} 

我认为这是更好的。

0

它看起来像你可能正在尝试传递一个现有的列表被填充或如果没有传入,然后读取功能试图分配和创建一个列表。这两种情况看起来都不错。

如果它是第一个(传入现有列表),那么while(current->next != NULL)循环将扫描到它的结尾。如果你正在尝试创建一个新的列表,那么看起来需要做一些额外的工作来将新节点链接在一起。

+0

你能帮我用CODE吗?不仅单词? Tnx在先进 – ScReYm0 2010-05-17 00:17:51