Advertisement

Writing files with pointers, uhh...

Started by September 18, 2000 04:58 PM
2 comments, last by cow_in_the_well 24 years, 3 months ago
Okay, I have some structs as follows...

typedef struct fooStruct
{
  int fooNumber;
  int *fooPointerToANumber;
  
  struct fooStruct *next; // Linked list stuff
  struct fooStruct *prev;
} fooStruct;

typedef struct fooListStruct
{
   char fooSomeText[32];

   fooStruct  *head;
   fooStruct  *tail;
} fooListStruct;
 
ok, now I create a fooStructList *list; I fill up the list then I want to save it to a file using WriteFile(hFile, list, sizeof(fooStructList),&dwNumWritten, NULL); Now, there are a few problems. 1. The file create is WAYY too small 2. The data won''t load properlly. I''ve noticed before that saving a struct that has pointers in it scambles the pointers, etc. Whats the proper way of saving a linked list to disk? Would I have to loop through the list and save each node to the file individually? thanks ----------------------------- -cow_in_the_well http://cowswell.gda.ods.org/ - Don't Panic -

- Thomas Cowellwebsite | journal | engine video

Okay. Sorry but this just will not work.

When you ask it to write that list to a file. What it writes is:
32 char''s - the some text
and 2 pointers - the head and tail

because thats exactly what the list has in it, so thats exactly what it writes.

your going to have to do something different:
write(list.fooSomeText); //write your text thingfooStruct *fooPointer = list.head; //point at the first foowhile(fooPointer != list.tail){write(*fooPointer) //write a foo from the listfooPointer = foopointer->next; //point to the next foo}; //rinse and repeat until they are all written 


likewise when you load them up your going to have to take care to load them one by one and link them all up

I think therefore I code.
Just because the church was wrong doesn't mean Galileo wasn't a heretic.It just means he was a heretic who was right.
Advertisement
Okay, that''s logical enough...but for the fooPointerToANumber in each node, do I have to write that seperately? Or will it be saved correctly with the node?

thanks

-----------------------------
-cow_in_the_well

http://cowswell.gda.ods.org/


- Don't Panic -

- Thomas Cowellwebsite | journal | engine video

again, it will just save a pointer which is useless to you when you load it up again.

If you find you use tons of pointers in your structs/classes there are good ways to deal with this. Add a function to each struct/class that writes itself to a file. This is often called serialization (dont ask me why.)

So rather than saying:
write(list)
use something like this:
list.write(filename)

this write function would call a .write() function for each entry in the list and everyone would take care of themselves. You would just have to write a similar .read() function that reads stuff in the proper order.

But this is alot of work, if you only use some pointers, its better off saying
fooPointer = list.first;
write(fooPointer->fooPointerToANumber);

I think therefore I code.
Just because the church was wrong doesn't mean Galileo wasn't a heretic.It just means he was a heretic who was right.

This topic is closed to new replies.

Advertisement