Writing linked lists to files

This is a discussion on Writing linked lists to files within the C Programming forums, part of the General Programming Boards category; Can you use fread and fwrite to write linked lists to an external file? I know how to use fread ...

  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,386
    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, 12: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, 05:10 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

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