Blockread/Blockwrite question. 
Author Message
 Blockread/Blockwrite question.

I've written the following program.


program CopyFile;
  FromF, ToF: file;
  NumRead, NumWritten: Word;
  Buf: array[1..2048] of Char;
  Assign(FromF, ParamStr(1));
  Reset(FromF,1); <----------------------------------(if val2 = 1)
  Assign(ToF, ParamStr(2));
  Rewrite(ToF, 1); <----------------------------------(if val1 = 1)
  Writeln('Copying ', FileSize(FromF), ' bytes...');
    BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
    BlockWrite(ToF, Buf, NumRead, NumWritten);
    writeln('----> ',numread:3,' ',numwritten:3);
  until (NumRead = 0) or (NumWritten <> NumRead);

Actually, I copied it from the BLOCKREAD listing in the help file. I
also created a.txt shown below.


There are 31 numbers in a.txt, but a.txt is 39 bytes in size because of
the carriage returns and line feeds
at the end of each line. Notice that the recsize that I'm using in my
reset and rewrite statements is both
= 1. I've experimented with different values in the reset and rewrite
statements. I've noticed that when
rewrite has say, val1 and reset has val2 and when you have a.txt and
b.txt as the command line
parameters, then the filesize of b.txt is equal to (filesize(a.txt) div
val1) * val2. For example, if you
have reset(FromF,8) and rewrite(ToF,3) in the above program, and the
filesize of a.txt is 39, then the
filesize of b.txt is (39 div 8) * 3 = 12.

Well, here is my question. I've noticed that if you are using a.txt as
your first command line parameter
and val1 = 32, then the filesize of b.txt is 0. I've even tried
experiments with another file called c.txt
which has a filesize of 135. If I type copyfile c.txt b.txt and val1 has
a value of 32, 64, or 128 (in both
cases, it doesn't matter what val2 equals) then filesize(b.txt)=0. Can
anyone tell me why? TIA. :-)

Patrick D. Rockwell

Fri, 23 Apr 2004 07:26:37 GMT  
 Blockread/Blockwrite question.

Hi Patrick,

You seem to have some fairly fundamental misconceptions about the
way record sizes are handled, and in particular how they relate
to BlockRead and BlockWrite.

You cannot simply change the record size and expect everything
else to work. Much of the code in the example is dependent on
the record size being 1 (as it must be for a procedure that can
be used with an arbitrary file and needs to use the complete file).

>   Writeln('Copying ', FileSize(FromF), ' bytes...');

This is the first mistake. FileSize returns the size in records,
not in bytes. If the record size is 1 it is the same thing, but
in general it is not (and in particular, when you change val1 it
is not). For instance, using your 39-byte file with a record size
of 2 shows the message 'Copying 19 bytes...' but copies 38 bytes.

>     BlockRead(FromF, Buf, SizeOf(Buf), NumRead);

This is also a mistake when the record size is not 1 (and this is
causing the behaviour that is puzzling you). SizeOf(Buf) returns
the size of the buffer in bytes. The 'Count' parameter in BlockRead
to which you are passing the size of the buffer is the number of
records that should be read. You need something like
SizeOf(Buf) div val1 here to get the correct number; else you are
in danger of overrunning the memory allocated to Buf.

Also, the help for Blockread says:

  The entire transferred block occupies at most Count symbol *
  RecSize bytes, where RecSize is the record size specified when
  the file was opened (or 128 if the record size was not
  specified). An error occurs if Count symbol * RecSize is
  greater than 65,535 (64K).

Evaluate 2048 * 32 and you will be enlightened.

>     BlockWrite(ToF, Buf, NumRead, NumWritten);

This is also a mistake if val1 and val2 are not equal. You
will write only some of the read information if val2 is
smaller; you will write garbage information if val2 is

>   until (NumRead = 0) or (NumWritten <> NumRead);

This condition also assumes the two files have the same record size.

The Scarlet Manuka

Fri, 23 Apr 2004 14:44:36 GMT  
 [ 2 post ] 

 Relevant Pages 

1. Blockread/Blockwrite question


3. Urgent help needed with GFx mode and BlockRead/BlockWrite

4. blockread/blockwrite

5. Blockread / Blockwrite


7. Problems using seek and blockwrite

8. Blockwrite

9. blockwrite anomaly?

10. BlockWrite

11. BlockWrite problem.

12. Blockwrite constant


Powered by phpBB® Forum Software