Name
BlockRead Procedure
Syntax
procedure BlockRead(var F: File; var Buffer; Count: Integer); procedure BlockRead(var F: File; var Buffer; Count: Integer; var RecordCount: Integer);
Description
Call BlockRead to
read Count records from a binary file into
Buffer. If F is an untyped
file, BlockRead uses the record size that you
specified when opening the file with Reset. If you
supply the RecordCount variable,
BlockRead stores in it the number of records
actually read. In the case of an error or end of file,
RecordCount might be less than
Count. BlockRead is not a real
procedure.
Errors
If you do not supply the
RecordCountargument, andBlockReadencounters an error or end of file, it reports I/O error 100.If the file is not open,
BlockReadreports I/O error 103.
Tips and Tricks
The
Bufferargument is not a pointer, but an untypedvarparameter. Pass the actual variable, not its address. If you have a pointer to a dynamically allocated buffer, dereference the pointer when callingBlockRead.The two most common uses for
BlockReadare to read many records at once and to read complex data structures that do not fit neatly into a simple typed file. For example, suppose a file contains a four-byte string length, followed by the string’s contents, and you want to read the data into a long string. In that case, you need to read the length and the string contents separately, as shown in the example.
Example
// Read a string from a binary file. // The string begins with a four-byte length. function ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access