[comp.lang.pascal] I/O with complex datastructures

jasonf@cetemp.Eng.Sun.COM (Jason Freund) (08/07/90)

	I've never dealt with file I/O in Pascal and I have a question
about saving a complex datastructure.  Basically, I have a single, very
complex datastructure that holds just about all of the information in my
program.  I want to save all of its data into a file so that I can load
it back up when the program's user wants to change some info in the database.
I was considering C until I learned that to do this, I would have to create my
own file format and step through each cell of the structure using getc/putc to
rewrite the data each time I wanted to modify it.  That would be impractical
for the complexity of my data.
	I was skimming through the Pascal book and it seems like there is some
way you can use reset/rewrite to save all data in a datastructure for reading
in later.  Is that right?  Could someone give me a simple example of how to
automatically save a piece of complex data (ie. a record w/ 2+ fields, any 
recursive structures, etc).  Are there any limitations/disadvantages to this?

Thanks,
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
-------------------------------------------------------------------------------
STOLEN QUOTES -- Please give the authors credit if you know who they are!    
"To understand recursion, you need to understand recursion."
"Wow!  Virtual memory!  Now I'm gonna build me a REALLY big ram disk!"
"My other computer is a SUN3/50."  "E. Pluribus UNIX"   -- authors unkown 

ajayshah@aludra.usc.edu (Ajay Shah) (08/07/90)

In article <140232@sun.Eng.Sun.COM> jasonf@cetemp.Eng.Sun.COM (Jason Freund) writes:
>
>	I've never dealt with file I/O in Pascal and I have a question
>about saving a complex datastructure.  Basically, I have a single, very
>complex datastructure that holds just about all of the information in my
>program.  I want to save all of its data into a file so that I can load
>it back up when the program's user wants to change some info in the database.

---------------------------------------------------------------------------
type
	anytype = blahblahblah

procedure savetodisk(fn:fnstring; var r:anytype);  {var for speed}
var f: file of anytype;
begin
	assign(f, fn); rewrite(f); write(f, r); close(f)
end;
---------------------------------------------------------------------------

Strengths: speed; it's very, very fast.

Caveat: you have to thread through complex pointer-oriented
	data-structures "by hand" and then you could easily hit trouble
	since TPascal isn't good at handling files containing hetrogenous
	objects.  Amazingly, the best solution in that case is TP 5.5's
	object-oriented implementation of a stream concept.  It's very
	beautifully done, and is a pleasure to use.

Minor point: you could lookup sizeof(r) to know how big the file
	is going to be.


-- 
_______________________________________________________________________________
Ajay Shah, (213)747-9991, ajayshah@usc.edu
                              The more things change, the more they stay insane.
_______________________________________________________________________________

winfave@dutrun.UUCP (Alexander Verbraeck) (08/16/90)

The solution has already been pointed out: create a file of the type
of your datastructure, open it, write your data, and close it. Very
easy indeed.

You asked about disadvantages: yes, there is at least one: if you use
pointers in your datastructure, you can't retrieve them, because you
never know whether your data will be loaded into the same memory
addresses. If you're not using pointers, the above method works just
fine.

----------------------------------------------------------------------
Alexander Verbraeck                  e-mail: winfave@dutrun.tudelft.nl
Delft University of Technology               winfave@hdetud1.bitnet
Department of Information Systems            winfave@dutrun.uucp
PO Box 356, 2600 AJ  The Netherlands         dutrun!winfave@hp4nl.uucp
----------------------------------------------------------------------