Path: utzoo!utgpu!news-server.csri.toronto.edu!me!mipscan Newsgroups: comp.lang.pascal From: rio@tension.me.utoronto.ca (Oscar del Rio) Subject: Re: more fun things to do with files Message-ID: <90Aug16.193133edt.62@tension.me.utoronto.ca> Organization: University of Toronto, Mechanical Engineering, Canada References: <6789.26caaab3@jetson.uh.edu> Date: 16 Aug 90 23:34:03 GMT In article <6789.26caaab3@jetson.uh.edu> chem4m@jetson.uh.edu writes: > If i have TWO datastructures w/ NO pointers, and I know that i am only going > to save one of each and accordingly will read only one of each, How would > I save these both into ONE file. The following program can help. It is necessary to define a third dynamic data structure of type RECORD (as DataType below) which contains both data structures. (I don't remember how this type of record is called). Well, in fact, this record will contain only ONE of the structures at a time, and its size is the size of the greatest data structure + 1 (the flag). If you are using TP 5.5, test it with the debugger (F8) and with Data1, Data2 and Data in the Watch Window. This method can be used for any number of data structures. ---- CUT HERE ---- program Test; type Data1Type = record { First data structure (any type) } a : string[30]; b : byte; c : set of char; end; Data2Type = record { Second data structure (any type) } d : string; e : real; f : char; end; DataType = record { Dynamic data structure } case flag:byte of 1: (D1:Data1Type); 2: (D2:Data2Type); end; var Data1 : Data1Type; Data2 : Data2Type; Data : DataType; f : file of DataType; begin FillChar (Data,SizeOf(Data),0); FillChar (Data1,SizeOf(Data1),0); FillChar (Data2,SizeOf(Data2),0); Data1.a := 'Structure 1'; Data1.b := 1; Data1.c := ['a','b','c']; Data2.d := 'Structure 2'; Data2.e := PI; Data2.f := '2'; (*** To save the data : ***) Assign (f,'fname'); Rewrite (f); Data.D1 := Data1; { to save the first data structure } Data.flag := 1; write (f,Data); Data.D2 := Data2; { to save the second data structure } Data.flag := 2; write (f,Data); Close (f); FillChar (Data,SizeOf(Data),0); FillChar (Data1,SizeOf(Data1),0); FillChar (Data2,SizeOf(Data2),0); (*** To read the data : ***) Assign (f,'fname'); Reset (f); read (f,Data); { read the first record } if Data.flag=1 then { is it Data1 or Data2 ? } Data1:=Data.D1 else Data2:=Data.D2; read (f,Data); { read the second record } if Data.flag=1 then { is it Data1 or Data2 ? } Data1:=Data.D1 else Data2:=Data.D2; Close (f); end. ---- CUT HERE ---- Hope this helps! ===================================== Oscar Ivan del Rio. Department of Mechanical Engineering University of Toronto. =====================================