[comp.lang.pascal] FILES and pointers

phys169@csc.canterbury.ac.nz (04/22/91)

In article <1991Apr22.014013.7390@usenet.ins.cwru.edu>, wct@po.CWRU.Edu (William C. Thompson) writes:
> 
> Say I want to save a dynamically sized structure....
> The problem is that I need to save this to disk so that it can
> be recalled as quickly as possible. 

If you use Turbo Pascal, the most efficient method is simply an untyped file, 
and use blockwrite to output the appropriate number of bytes, e.g.

type BufferType = array[1..65520] of byte;
var BufferPtr : ^BufferType;
    MyFyle    : file;
begin
assign(MyFyle,'something'); rewrite(MyFile,1);
getmem(BufferPtr,some-number);
blockwrite(MyFyle,BufferPtr^,some-number);
end.

In generic Pascal, you might have to either issue many writes (e.g. FILE OF 
CHAR) or write too much (e.g. FILE OF BUFFER, where bufferptr was declared as 
a pointer to type Buffer), it all depends on the implementation's enhancements
if you're after efficiency.

Hope this helps,
Mark Aitchison.

IO92203@MAINE.BITNET (Scott Maxell) (04/23/91)

>Say I want to save a dynamically sized structure.  People
>have suggested linked lists, but those can be very slow.
>Someone else suggested the following:
>
>type
>  bufferptr=^buffer;
>  buffer=array[1..64000] of byte;
>var
>  p: bufferptr;
>begin
>  getmem(p,(required size));
>  ...
>  freemem(p,(required size));
>  ...
>end.
>
>The problem is that I need to save this to disk so that it can
>be recalled as quickly as possible.  Instead of a text file
>with numbers written on it (way too slow and too big), I need
>a FILE of something.  But a FILE of what and how large will the
>file be?
>
>Ideas:
>
>FILE of bufferptr
>   dumb, it only saves address
>
>FILE of bufferptr^
>   is this even legal?  if it is, this looks like the
>   proper solution
>
>FILE of buffer
>   that has way to much disk storage space.  I need to
>   minimize both disk space and heap space required
>   although minimizing disk storage space seems a bit
>   more important
>
>Any suggestions or ideas?
 
 
  You might try using an untyped file and BlockRead/BlockWrite.
To open the untyped file, you should specify a record length of 1
since you want a file of BYTE.
 
File is declared:  FileVar : File;
 
ASSIGN (FileVar, 'filename.ext');
RESET  (FileVar, 1);
 
To read the file, you would use:
 
BLOCKREAD (FileVar, Buffer, SizeOf (Buffer), Result)
 
Result:            Result  : Word;
 
Result returns the number of bytes actually read from the file if it
differs from SizeOf (Buffer).
 
BlockWrite works the same way:
 
BLOCKWRITE (FileVar, Buffer, SizeOf (Buffer), Result)
 
Result returns the number of bytes actually written.
 
Result in both BlockRead/write is optional. If you don't specify it, and
the number of bytes is not an exact multiple of the record size that
you specified in your Reset/Rewrite, you'll get an I/O error.
 
P.S. This is all done from memory, so you should look things up for more
     specific information on it.
 
//////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
+---------+ Scott Maxell  -- Bitnet   ->> IO92203 @ maine
|         |               -- Internet ->> IO92203 @ maine.maine.edu
|    O    |
|    |    | "What I need is a computer that will do what I want it to
+---------+ do, not what I tell it to do..."