I have simple file copy example:
var infile,outfile: file; buff: array[1..512] of byte; copied: integer; begin assign(infile, 'input'); reset(infile, SizeOf(byte)); assign(outfile, 'output'); rewrite(outfile, SizeOf(byte)); repeat blockread(infile, buff, 512, copied); blockwrite(outfile, buff, copied); until copied < 512; close(infile); close(outfile); end.
It works fine. But if before repeat loop I placed:
blockread(infile, buff, 1); blockwrite(outfile, buff, 1);
it copy only 16384 (2^14) bytes of input. Always. If input file is smaller than 2^14 bytes then it copied all bytes. If larger then it cutted down to 2^14 bytes.
I have this behaviour with GPC only. No problems with FPC, VP...
What is wrong?
Thanks in advance, Peter
__________________________________ Do you Yahoo!? Yahoo! Finance: Get your refund fast by filing online. http://taxes.yahoo.com/filing.html
Peter Norton wrote:
I have simple file copy example:
var infile,outfile: file; buff: array[1..512] of byte; copied: integer; begin assign(infile, 'input'); reset(infile, SizeOf(byte)); assign(outfile, 'output'); rewrite(outfile, SizeOf(byte)); repeat blockread(infile, buff, 512, copied); blockwrite(outfile, buff, copied); until copied < 512; close(infile); close(outfile); end.
It works fine. But if before repeat loop I placed:
blockread(infile, buff, 1); blockwrite(outfile, buff, 1);
it copy only 16384 (2^14) bytes of input. Always. If input file is smaller than 2^14 bytes then it copied all bytes. If larger then it cutted down to 2^14 bytes.
I have this behaviour with GPC only. No problems with FPC, VP...
What is wrong?
I cannot reproduce it, but I suppose it's just a misunderstanding of the `BlockRead' semantics. If the 4th parameter is given, it doesn't have to read the whole buffer because the 4th parameter tells how many were read. (This is useful, e.g., when reading from pipes and other inputs were data may become available after some time. You usually don't want to wait until exactly 512 bytes are avaible, but read what is there now and know how much was read. Of course, since Dos doesn't have pipes, this issue didn't come up in BP.)
So, change `< 512' to `= 0', and it should work fine. (It will also work in BP, of course, and the check is also easier -- if you ever change the buffer size, you don't have to remember to check it there, too.)
If you really want to read the whole buffer (not here), just omit the 4th parameter and possibly check for I/O errors.
Frank