Thread: need help with file I/O and double linked-list

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    5

    need help with file I/O and double linked-list

    I'm trying to set up a double linked-list, that is a linked-list within a linked-list. the structure looks like this:

    Code:
    struct Dat
    {
          int acc;
          int spd;
          Dat *nextdat;
          Dat *prevdat;
    };
    
    struct Node
    {
          char name [64];
          char pass [16];
          int avg_acc;
          int avg_spd;
    
          Dat *dat,
                *dat_h,
                *dat_t;
    
          Node *next,     
    	*prev;
    };
    
    Node *node, //current node
             *list_h, //head
             *list_t; //tail
    my problem is saving the entire structure to a file, and then being able to open it up later. Saving the initial linked-list isn't a problem. But the pointers within the first linked-list which create the second linked-list may be a problem. Also, I can't come up with a successful way to get the data from the file and back into a linked list. My initial idea was to set up an initial node, open the file, save the data from the first node to the initial node, then create a new node, save the next data to that node, and so on until the eof is encountered. The problem is getting the second linked-list into the file, and back out. Here is my attempt at saving the file:

    Code:
    void LList::SaveToFile ()
    {
          int i;
    
          ofstream data ("data.dat");
    
          node = list_h;
    
          while (node)
          {
    	data.write((char*)node, sizeof(Node));  			node = node->next;   //does this write the internal linked-list as well? 
          }
    
          data.close ();
    }
    Can anyone help me with the syntax for this file operation or point me somewhere that describes this type of problem?
    Last edited by jamie; 02-20-2003 at 08:09 PM.

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    theoretically speaking you're on the right track...

    when it comes to saving linked list to a file, a "rule of thumb" is to only save the "data" portion of a linked list (of each node) into a file -----> not the entire node nor pointers associated with it, b/c the next time you run your program those pointers will have an address of a memory location that is not associated with your program, that's why it's called RAM (random access memory)

    the same pertains to reading in your values....like you stated you create a new node and read in DATA from the file and insert it into each data member of a particular node....


    anyways..........when it comes to having a linked list inside of a linked list (that is a node having a pointer to a node of completely different type) i would recommend creating a seperate file and storing DATA of each node of that linked list into that second DIFFERENT file..........

    after all, there are other ways of doing this as well, but i think this one might be one of the easiest...


    hope that helps

    ...............
    matheo917

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    5
    This helped alot. Thank you sooo much. I was trying to save the entire node into the file, without really even thinking about memory allocation, etc... Thanks again for the help.

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    glad I could help

    ......................
    matheo917

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Inserting new nodes in a sorted double linked list
    By carrja99 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2003, 08:34 AM
  5. file i/o -linked list
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2001, 11:13 PM