If you have a large structure made up of different types (e.g. int, char, float, enum, etc), sub-structures,
pointers, arrays and single elements stored in a binary file what is the best/portable way of (saving and)
reading it into a structure, when you don't know the size of the structure data in the binary file beforehand.
To save the structure I did a fwrite like
fwrite ((void*) &ptr_structure,1, bytes_to_write, (FILE *)ptr_file);
It seems to have worked. An output file was created and when I viewed the binary file at least the right strings
were in there.
But I can't seem to be able to read the file and repopulate the structure. I can't use a fread since I
won't know the size of the structure data in the file, so I used a fgetc() and tried to read it character by character using
while ( !feof(file_handle)
*ptr_structure = (char)fgetc(file_handle);
Although it doesn't report an error, when I tried to find the earlier strings they weren't where they were supposed
to be, or anywhere else? Am I missing some form of alignment? Or is the fwrite I did wrong in the first place.
According to the C FAQ (http://www.faqs.org/faqs/C-faq/faq/
) (Question 2.11) using a fwrite means the file
is not portable, in particular pointer information is lost or distorted. Also, (when the size of the structure
is not known) the only reliable way of reading in a structure is element by element. Are these really the
case? What do people do when they have a large structure?