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 RecordCount argument, and BlockRead encounters an error or end of file, it reports I/O error 100.

  • If the file is not open, BlockRead reports I/O error 103.

Tips and Tricks

  • The Buffer argument is not a pointer, but an untyped var parameter. Pass the actual variable, not its address. If you have a pointer to a dynamically allocated buffer, dereference the pointer when calling BlockRead.

  • The two most common uses for BlockRead are 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 ReadString(var F: File): string;
var
  Len: LongInt;
begin
  BlockRead(F, Len, SizeOf(Len));
  SetLength(Result, Len);
  if Len > 0 then
    BlockRead(F, Result[1], Len);
end;

See Also

AssignFile Procedure, BlockWrite Procedure, CloseFile Procedure, IOResult Function, Reset Procedure, Rewrite Procedure, $I Compiler Directive, $IOChecks Compiler Directive
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset