read/write linked list

This is a discussion on read/write linked list within the C Programming forums, part of the General Programming Boards category; I want to write the data that the linked list holds to disk. I believe the function that writes the ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    read/write linked list

    I want to write the data that the linked list holds to disk. I believe the function that writes the data is code correctly. Its reading that I have problems with. I take my problem is that headRef is local. How would I get around this?

    Code:
    struct node {
       int data;
       struct node* next;
    };
    
    int saveList(struct node** headRef, char* filename)
    {
       struct node* current = *headRef;
       FILE *fp;
       if(current == NULL)
          return 1;
       fp = fopen(filename,"wb");
       if(fp == NULL)
          return 2;
       while(current != NULL)
       {
          fwrite(&current->data, sizeof(struct node), 1 , fp);
          current = current->next;
       }
       fclose(fp);
       return 0;
    }
    /**********************************************************/
    int loadList(struct node** headRef, char* filename)
    {
       struct node* current = *headRef;
       int data;
    
       FILE *fp = fopen(filename,"rb");
       if(fp == NULL)
          return 1;
       while(fread(&data, sizeof(struct node), 1, fp) != 0)
       {
          addToList(&current,data);
       }
       fclose(fp);
       return 0;
    }
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    You are writting too much data: "fwrite(current->data,sizeof(struct node),1,fp)" - the size of the structure is not the size of data.

    gg

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    so what you are saying to use sizeof(int) instead of sizeof(struct node)?
    [edit]
    Ok, so I did that. After deleting the list and then loading it back from disk it says the data isn't being added to the list.

    [/edit]
    Last edited by lambs4; 03-29-2003 at 02:29 PM.
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    If data is an int, then yes.

    gg

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Ok, so I did that.
    Did you do it on both the fread and fwrite?

    Post your revised code if you still have problems.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-04-2006, 05:39 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21