On Mon, 23 Jan 2006 23:04:50 -0500, Allin Cottrell <co******@wfu.edu>
wrote in comp.lang.c:
A. Suppose my program mallocs and populates an array of structs
of a fixed size apiece. For example, something like:
struct foo {
int i;
double x[8];
char s[16];
};
We want to save this work and re-read it later. One solution
that works well -- so long as we're sure the re-reading will
occur on the same platform as the saving -- is to do a binary
dump of the array of structs with fwrite(), then read them
back into memory later with fread().
B. Now suppose my program mallocs and populates an array
of structs which themselves contain dynamically allocated
members, for example
struct baz {
int i;
double *x; /* size of array unknown at compile time */
char *s; /* length of string unknown at compile time */
};
Question: Is there any fancy footwork which will permit the
use of a similar approach to case A -- in terms of fwrite()
and fread() -- in this second case?
Allin Cottrell
Wake Forest University
Please use a proper signature delimiter, like mine below. It consists
of three characters "-- ", that is two dashes and a space. Most
newsreaders recognize it and handle it properly automatically when
quoting.
As to your question, you can't portably save pointers to a file and
expect the same memory to be usable again when you read them back. For
the array of doubles, I would suggest writing an int first that
specifies how many doubles are to follow, then the doubles themselves.
You can take a similar approach with the character string, or since
you will know when the string starts, you can just dump the string and
its '\0' terminator.
But you will certainly need to decide on some extra work, to be done
when writing and undone when reading back in.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html