views:

78

answers:

2

Ok i have problem with my code for reading binary file...

First i will show you my writing code:

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

and here is my "version" for reading:

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

I just need to save my linked list in binary file and to be able to read it back ... please help me. The program just don't read it or its crash every time different situation ...

+2  A: 
  1. Your do..while loop could be formed better. If you're going to check at the end, don't check at the beginning too. If you find you have to do that, you are probably not using the correct flow control. For example, here you should just be saying while(current != NULL) { }

  2. What are you trying to do with if(current != NULL) { }? You are setting the current node in your loop to a brand new BOOK, and making his next element NULL. Why? Why not just mirror the loop you have in the writing method?

  3. Look at what you are doing if current == NULL implicitly - you are strcpying in your reading method. Don't do that.

  4. You seem to be saying fclose(in) within the while loop in book_open.

I'll get more once I compile it.


Ok, I edited the code somewhat making 2 assumptions

  1. This isn't a homework problem
  2. BOOK only has 1 pointer (next) and everything else is an array with memory allocated to it

book_saving - simply loops and writes

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 - takes a pointer to a pointer to 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;
}

I think that's better.

Phil
A: 

It looks like you might be trying either to pass in an existing list that gets filled in or if one is not passed in, then the read function is trying to allocate and create a list. Neither situation looks quite right.

If it is the first (passing in an existing list), then the while(current->next != NULL) loop will scan to the end of it. If you are trying to create a new list, then it looks like that needs to do some extra work to link the new nodes together.

Mark Wilkins
Can you help me with CODE ??? Not only words ??? Tnx in advanced
ScReYm0