[comp.lang.pascal] TURBO PASCAL: fancy file handling, please

amead@s.psych.uiuc.edu (alan mead) (06/28/91)

I have an application written in TPv5.0 that needs several files.  I am
loath to increase their number.

I already store one complex data type in a FILE OF COMPLEXREC; and now
need to store another.  I would prefer to store both in a single file.
The data types are of different sizes, and I need to store 4-6 of the
first kind for each of the second type.

Is there a relatively simple way to treat a single file as a number of
concatenated files (of differing format)?  I wouldn't mind rewriting
the Seek() routine if I could do it in TP (rather then assembly).

Thanks much.

-alan mead : amead@s.psych.uiuc.edu

pshuang@athena.mit.edu (Ping-Shun Huang) (07/01/91)

In article <1991Jun27.231235.2861@ux1.cso.uiuc.edu> amead@s.psych.uiuc.edu (alan mead) writes:

 > Is there a relatively simple way to treat a single file as a number of
 > concatenated files (of differing format)?  I wouldn't mind rewriting
 > the Seek() routine if I could do it in TP (rather then assembly).

Assumption: you may want to write aritrary number of TypeA, followed by
arbitrary number of TypeB, etc.  Obviously if you already write X of one
and Y of another, creating an appropriate RECORD would be an easy solution.

(a) I think Turbo Pascal 6.0/5.5(?) may support a List object which
permit you to store arbitrary types into a single disk file.

(b) I have not tried this, but I wonder if Turbo Pascal lets you ASSIGN
the same filename to two separate file variables, which could be of
different types.  If you padded out your data records so that the two
types are multiples of each other, with a little arithmetic (and some
sugar-coating procedures and functions you could write) you could do
what you want.

If you don't wish to pad out your records, or if TP won't let you ASSIGN
two file variables to the same filename, the more general solution would
be for you to open the file as a FILE OF BYTE and use BlockRead and
BlockWrite to read and write data.  What you would want to do is add a
tag field to the beginning of RECORD types A and B.  BlockRead a portion
of the file into a buffer as large the larger of A or B, then checks the
tag field to see what it really is.

Being able to seek to an arbitrary record would require sequentially
accessing all the objects in between to determine the type and therefore
how far to skip ahead in the file for the next object, or would require
that you maintain a separate index file -- shouldn't be hard.

--
Above text where applicable is (c) Copyleft 1991, all rights deserved by:
UNIX:/etc/ping instantiated (Ping Huang) [INTERNET: pshuang@athena.mit.edu]