Thread: Writing linked lists to files

  1. #1
    Unregistered
    Guest

    Question Writing linked lists to files

    Can you use fread and fwrite to write linked lists to an external file? I know how to use fread and fwrite to write a structure, but what is the proper code for linked lists.

    I have a feeling it will have something to do with *first, *current, *new structure variables but can't quite figure it out.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You could loop through the list, writing each node to a file. However, the pointers that join the nodes are going to be invalid when you read them back in, so special care will be needed. (They are only valid for each run of the prog, when they are in memory. New memory locations (pointers) will be assigned when you read nodes back in).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You can store the data to the file. But when you are reading the file again, I think you have to build up the tree again. Pointers are only valid for the time running. When the program goes running again, different addresses will be use, so the pointers will be invalid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions on Linked Lists
    By Weng in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2003, 01:17 AM
  2. Question On Linked Lists
    By Zildjian in forum C Programming
    Replies: 8
    Last Post: 10-23-2003, 11:57 AM
  3. files and linked lists
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-13-2002, 07:49 AM
  4. Replies: 1
    Last Post: 03-21-2002, 06:10 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM