Article View: comp.sys.amiga.tech
Article #4722Re: I/O of complex data structures in C
From: Chuck McManis
Date: Mon, 06 Aug 1990 04:39
Date: Mon, 06 Aug 1990 04:39
41 lines
2242 bytes
2242 bytes
In article <140087@sun.Eng.Sun.COM> (Jason Freund) writes:
> When you save data in a database, does the program just go:
>"fwrite(pointer, sizeof, *pointer, items, stream)" which somehow magically
>saves every piece of data (specified in the arguments) in such a way that it
>will be able to read in every piece of data back into their correct cells in
>the data structure? That is what I want to do -- and I want to know if fread
>and fwrite can do it.
No, they can't do that. You see they context of what the pointers mean is
lost in the file. fread/write work fine for "flat" datastructures, ie ones
that don't have pointers in them. You need to marshall the data before
writing it to disk and that means some sort of code that understands your
data structures.
You can cheat a bit (and save yourself some coding time) by using XDR
(as in Sun RPC/XDR) to marsall the data into a byte stream and write
that stream to disk. Unmarsalling is accomplished by the same functions
so you would do something like :
myfile = fopen("level1.xdr", "w");
xdrstdio_create(&xdrs, myfile, XDR_ENCODE);
xdr_foo_struct(&xdrs, &foo_struct);
fclose(myfile);
xdr_destroy(&xdrs);
myfile = fopen("level2.xdr", "r");
xdrstdio_create(&xdrs, myfile, XDR_DECODE);
xdr_foo_struct(&xdrs, &foo_struct);
fclose(myfile);
xdr_destroy(&xdrs);
Source to the rpc/xdr package is available on rice.titan.edu for anonymous
ftp. Porting the XDR routines to the amiga is fairly trivial.
--
--Chuck McManis Sun Microsystems
uucp: {anywhere}!sun!cmcmanis BIX: <none> Internet: cmcmanis@Eng.Sun.COM
These opinions are my own and no one elses, but you knew that didn't you.
"I tell you this parrot is bleeding deceased!"
Technical Details
Message-ID:
<140206@sun.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!usc!apple!sun-barr!newstop!sun!stpeter.Eng.Sun.COM!cmcmanisReferences:
<140087@sun.Eng.Sun.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!usc!apple!sun-barr!newstop!sun!stpeter.Eng.Sun.COM!cmcmanis From: cmcmanis@stpeter.Eng.Sun.COM (Chuck McManis) Newsgroups: comp.sys.amiga.tech Subject: Re: I/O of complex data structures in C Summary: No, it won't work. Message-ID: <140206@sun.Eng.Sun.COM> Date: 6 Aug 90 04:39:49 GMT References: <140087@sun.Eng.Sun.COM> Sender: news@sun.Eng.Sun.COM Organization: Sun Microsystems, Mt. View, Ca. Lines: 41