[net.unix] C help wanted

becker@uiucdcsb.UUCP (11/04/84)

 i was wondering if any of you C hackers out there might be able
to help me on this:

 i have a program that generates a rather complex model of an object
(this is for computer graphics) that is composed of records. each record
contains data and pointers to other records. this entire data structure
is irregular. how can i save this structure in a file so that it can
be used by another program? keep in mind that this structure is not
tree-like in any way; it more closely resembles something a spider did
under influence of drugs.

 thanks,
 craig becker

 uiucdcs!uiucdcsb!becker

dave@utcsrgv.UUCP (Dave Sherman) (11/07/84)

In article <19300024@uiucdcsb.UUCP> becker@uiucdcsb.UUCP (Craig Becker) writes:
~| 
~|  i have a program that generates a rather complex model of an object
~| (this is for computer graphics) that is composed of records. each record
~| contains data and pointers to other records. this entire data structure
~| is irregular. how can i save this structure in a file so that it can
~| be used by another program? keep in mind that this structure is not
~| tree-like in any way; it more closely resembles something a spider did
~| under influence of drugs.

Store all the data in structures declared in consecutive locations
in memory, and save it in a file with a single write(2) call with address
beginning at the start of all the records, and a size large enough to cover
all the data. Read it in the same way. It's cheating, but it works.

Dave Sherman
Toronto
-- 
 { allegra cornell decvax ihnp4 linus utzoo }!utcsrgv!dave

Ron Natalie <ron@BRL-TGR> (11/08/84)

The way I did that when I had the problem was to save each item pointers
and all into the record (along with the address of each one tacked on to
the front of the record).  When I loaded them back in, I constructed a
table of old-address to new-address mappings.  I then went through each
one and fixed the pointers.

-Ron

rbt@sftig.UUCP (R.Thomas) (11/11/84)

> ~|  i have a program that generates a rather complex model of an object
> ~| (this is for computer graphics) that is composed of records. each record
> ~| contains data and pointers to other records. this entire data structure
> ~| is irregular. how can i save this structure in a file so that it can
> ~| be used by another program? keep in mind that this structure is not
> ~| tree-like in any way; it more closely resembles something a spider did
> ~| under influence of drugs.
> 
> Store all the data in structures declared in consecutive locations
> in memory, and save it in a file with a single write(2) call with address
> beginning at the start of all the records, and a size large enough to cover
> all the data. Read it in the same way. It's cheating, but it works.
> 
One more thing that makes this a little easier is to keep all pointers
relative to the beginning of the area you will write out.  This way
you can read it all back in to a different porgram without having to
adjust all the pointers for the offset between the old beginning
address and the new one.

Rick Thomas

greenber@acf4.UUCP (11/13/84)

<>

Assuming that you need two or more different programs to access this data,
remember to subtract/add the offset of the beginning of these structures
BEFORE writing it out to disk and AFTER reading it in.  This makes the
data "position independent".  Another way of doing it (involves more work
during the run of the program, though) is to only have offsets to the
data in question stored instead of pointers.  That is offsets from some
arbitrary data address that is at the begiining of your structure and
also, again, this requires contiguous structures.


Ross M. Greenberg  @ NYU   ---->  allegra!cmcl2!acf4!greenber  <----