Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Using fread() and fwrite() to read and write a linked list to a file

 
Ranch Hand
Posts: 38
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

First, I want to mention that I already posted it on StackOverflow, and didn't get an answer yet.

I have a linked list that I want to write to a file. For this purpose, I wrote the following code:


Now, I want to take the file I extracted while using the last code and read the linked list. For this purpose, I wrote the code:

When I look at 'temp', I see a corrupted data structure. 'temp' points to a node that doesn't have the same fields as the original head of the linked list, and the 'next' field in temp points to NULL.

Can you try to tell me what I'm doing wrong here?
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's see if we can help you, Josh. First, I have edited your post to useCodeTags <-- click on this. Makes it much easier to read and discuss.

Second, look at Line 8 of your backup_list routine. It's writing out the structure pointed to by the address in head. You want each iteration of your while loop to write the next node in your list. That means the address in head has to change. Does it? If you think it does, tell me what line number to look at to see the code that changes it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic