Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!ames!decwrl!btr!eeh From: eeh@btr.BTR.COM (Eduardo E. Horvath eeh@btr.com) Newsgroups: comp.sys.amiga.tech Subject: Re: I/O of complex data structures in C Message-ID: <298@public.BTR.COM> Date: 3 Aug 90 20:11:29 GMT References: <140087@sun.Eng.Sun.COM> Organization: BTR Public Access UNIX, MtnView CA, Contact: cs@btr.com 415-966-1429 Lines: 43 [ Eat this you !#%&* line-eater ] In article <140087@sun.Eng.Sun.COM> jasonf@cetemp.Eng.Sun.COM (Jason Freund) writes: >-- somewhat hypothetical situation representing a real problem -- > Basically, a maze is a complex data structure (a 2D array of and array >of pointers to blah, blah... (it's deep)). So that means I want to use fread() >and fwrite() (right?) My programming book says *very* little on those ********** NO DON'T DO THAT!!!!! ************ fread() and fwrite() simply write out the image of whatever they point to. fwrite(char *b, int bsize, int n, FILE *fp) works like this: it will take whatever data is at location <*b> and put bytes of it into file <*fp>. This would work fine if the program was always loaded into the same place. On almost all computers (including the PC and Mac and especially (sp?) on the Amiga) programs are relocated when they are loaded because different parts of memory are being used for something else and the program must find other memory. You can never be certain that a program will load in the same place twice. The Amiga can have other programs that have grabbed previously free pieces of RAM. PC can have TSR's, and the Mac OS does run-time relocations of data. What does this mean to you? Your pointers will most certainly be well and truly trashed. Your database will be garbagy gobbldygook. What you must do is traverse your database and save each node separately. (fwrite() may work.) You must do this in a way that the pointers are not necessary for reconstructing the database. Then when you read it back in, your database must be carefully reconstructed by adding one node at a time. >Jason Freund, Sun Microsystems, jasonf@cetemp.Corp.sun.com <== summer address >Deprtmnt of Computer Science, Univ California, Davis. freund@sakura.ucdavis.edu >Quantum Link: JasonF5, Compu$erve: 72007,244, 690 Erie Dr, Sunnyvale, CA 94087 --------------------------------------------------------------------- Edurardo Horvath eeh@btr.com ..!{decwrl,mips,fernwood}!btr!eeh "Trust me, I know what I'm doing." - Sledge Hammer ---------------------------------------------------------------------