Board index » delphi » How do I use BLOCK READ / BLOCK WRITE

How do I use BLOCK READ / BLOCK WRITE

Hi I need a faster way to read and write to files than textfile. I do
not know how to use BLOCK READ or BLOCK WRITE.

Thanks.
Chris Lee
l...@netshop.net

 

Re:How do I use BLOCK READ / BLOCK WRITE


Quote
In article <4e9ccq$...@news.netshop.net> l...@netshop.net (Chris Lee) writes:
>From: l...@netshop.net (Chris Lee)
>Subject: How do I use BLOCK READ / BLOCK WRITE
>Date: Fri, 26 Jan 1996 00:44:56 GMT
>Hi I need a faster way to read and write to files than textfile. I do
>not know how to use BLOCK READ or BLOCK WRITE.

Although you can use blockread and blockwrite with text files, it might not
be the most effective way to handle the problem.  Reason:  Whenever you open
a file in TP to do anything with it, the control structure sets up a 128-
byte buffer to read information from a file.  This means to read one sector (
512 bytes) from a disk, you're loading up the buffer 4 times.  That adds up
to a lot of overhead!  Blockread and blockwrite can just as ineffecient if
handled correctly.  ;-)

I'd recommend the following solution (very similar to the sample in the TP7
Programmer's Guide):

program big_buffer;

type
  Tbuffer = array[0..16383] of byte; {a 16K buffer}
  Pbuffer = ^Tbuffer;

var
  f : text;
  textbuffer : Pbuffer;

begin
  assign (f, 'myfile.txt');
  new (textbuffer);
  settextbuf (f, textbuffer^, sizeof(Tbuffer));
  reset (f);
  {...do whatever needs to be done with the file...}
  close (f);
  dispose (textbuffer);
  {dispose the buffer ONLY after you're sure you won't need the text
   variable again!}
end.

This will give the variable f a 16K buffer to work with.  Result:  You're
loading the buffer once every 32 sectors instead.  Much less overhead.

Some notes:

1.)  DO NOT do this on a file that's already open and has been used for
I/O.  This can trash a file.  You can call settextbuf immediately after a
reset or rewrite safely, though.

2.)  Make sure you have enough heap space for the buffer, use the {$M #,#,#}
directive.

3.)  Every file you open needs its own buffer!

You could use blockread and blockwrite if you wanted to, but they'd be a
real pain.  For reading, you'd have to set up a buffer, grab characters from
the buffer until you reach CRLF, and stuff them into a string.  You also
have to worry about when to reload your buffer, taking into consideration
the possibility that you might be in the middle of a string.  For writing,
it's a little easier, since you can just forgo the buffer and write the
useful part of the string to the file and append a CRLF.

Of course, if this doesn't make sense, DEFINITELY use the settextbuf
procedure for textfiles.  Worry about blockread and blockwrite only if you
need to work with raw binary data.

Quote
>Thanks.
>Chris Lee
>l...@netshop.net

This might not answer the question you asked, but it should help with your
particular problem.  :-)

--
Scott F. Earnest / sc...@whiplash.res.cmu.edu | GO STEELERS!
Pascal Nut / Die-hard STEELERS Fan!           | '95-'96 NFL CHAMPIONS!  ;-)

Other Threads