9 Replies - 7254 Views - Last Post: 20 February 2010 - 01:46 PM Rate Topic: -----

#1 akhyls  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 19-February 10

Writing Linked list To A File

Posted 19 February 2010 - 11:24 AM

i want to write a linked list to a file in struct format.
for example
struct list
{
int no;
struct list * pointer;
};
struct list row;
if row has three nodes when i write
fwrite(row, sizeof(struct list),1,file)
does it write only the first node or all nodes
Is This A Good Question/Topic? 0
  • +

Replies To: Writing Linked list To A File

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Writing Linked list To A File

Posted 19 February 2010 - 11:28 AM

Crossposted also here.

It's bad etiquette to spam your question over multiple websites.
Was This Post Helpful? 0
  • +
  • -

#3 akhyls  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 19-February 10

Re: Writing Linked list To A File

Posted 19 February 2010 - 11:59 AM

View PostJackOfAllTrades, on 19 February 2010 - 10:28 AM, said:

Crossposted also here.

It's bad etiquette to spam your question over multiple websites.


Sorry for it :blush:

View PostJackOfAllTrades, on 19 February 2010 - 10:28 AM, said:

Crossposted also here.

It's bad etiquette to spam your question over multiple websites.



How can i change the title
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Writing Linked list To A File

Posted 19 February 2010 - 12:19 PM

It's not clear what you want to do here. You don't really want to save the memory addresses of the pointers in the struct I assume?
Was This Post Helpful? 0
  • +
  • -

#5 akhyls  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 19-February 10

Re: Writing Linked list To A File

Posted 19 February 2010 - 12:52 PM

View PostJackOfAllTrades, on 19 February 2010 - 11:19 AM, said:

It's not clear what you want to do here. You don't really want to save the memory addresses of the pointers in the struct I assume?



Homework wants us to do like this :)
Was This Post Helpful? 0
  • +
  • -

#6 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Writing Linked list To A File

Posted 19 February 2010 - 04:43 PM

You've misread it.

Saving the pointers themselves won't do anything; a pointer holds a memory adddress (0x00000001 to 0x7FFFFFFF), so you want to save the value of the pointer:
linked_list *p = &some_linked_list;
for(int i = p; i < p + sizeof(linked_list); ++p) {
That will iterate through all values contained in the some_linked_list class.
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Writing Linked list To A File

Posted 19 February 2010 - 07:45 PM

Generally when saving a linked list you will want to save only the data -- since the memory addresses would be no good next time you program builds a linked list.

So the problem becomes how to preserve the links? Well for a simple linked list you are in luck. The nodes for a nice chain that can just be saved in a linear structure.

for example if you had a list of integers: [10 -> 20 -> 45 -> 6 -> 15] -- well you just write out the list as:

10 20 45 6 15

The order tells you how the nodes were connected in memory.

This generally works for linked lists. Trees, graphs, hash-tables etc. are much more difficult and generally require some kind specialized indexed format.
Was This Post Helpful? 0
  • +
  • -

#8 akhyls  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 19-February 10

Re: Writing Linked list To A File

Posted 20 February 2010 - 09:54 AM

Thanks for your answers. You are right I think i understand the question wrong. And what does writing a list to a file in struct format mean?
Was This Post Helpful? 0
  • +
  • -

#9 akhyls  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 19-February 10

Re: Writing Linked list To A File

Posted 20 February 2010 - 12:19 PM

I understand the question this time much better. For example there is a structure like this :

struct list
{
int no;
struct list * pointer;
};
struct list row;

I want to write the elements in struct format. Should i write

fwrite(row->no,sizeof(struct list),1,file), or

fwrite(row->no,sizeof(int),1,file)
Was This Post Helpful? 0
  • +
  • -

#10 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Writing Linked list To A File

Posted 20 February 2010 - 01:46 PM

just write the int not the pointer...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1