Reads a block of data records from an untyped binary file System unit
 procedure BlockRead ( var FileHandle : File; var Buffer; RecordCount : Integer {; var RecordsRead : Integer} ) ;
The BlockRead procedure is used to read RecordCount data records into Buffer from an untyped binary file given by the FileHandle.
The file must have been assigned using AssignFile and opened with Reset.
The Reset routine by default will open the file with a record size of 128 bytes. This can be overriden in this routine to a value more useful to yourself (see example).
Data is written to the Buffer (normally a string or byte array) from the file. If the recordSize is 10 bytes, and RecordCount is 3, then 3 x 10 byte records are written, with 30 bytes taken from the file to do this.
The actual number of records read is stored in the optional RecordsRead variable, if provided. It will be less than RecordCount if, for example, the end of the file has been reached.
Related commands
BlockWrite Writes a block of data records to an untyped binary file
File Defines a typed or untyped file
Read Read data from a binary or text file
ReadLn Read a complete line of data from a text file
Reset Open a text file for reading, or binary file for read/write
Write Write data to a binary or text file
WriteLn Write a complete line of data to a text file
 Author links

 Download this web site as a Windows program.

Example code : Reading data from a binary file one byte at a time.
  myFile    : File;
  byteArray : array[1..8] of byte;
  oneByte   : byte;
  i, count  : Integer;

  // Try to open the Test.byt file for writing to
  AssignFile(myFile, 'Test.byt');
  ReWrite(myFile, 4);   // Define a single 'record' as 4 bytes

  // Fill out the data array
  for i := 1 to 8 do
    byteArray[i] := i;

  // Write the data array to the file
  BlockWrite(myFile, byteArray, 2);   // Write 2 'records' of 4 bytes

  // Close the file

  // Reopen the file for reading
  FileMode := fmOpenRead;
  Reset(myFile, 1);   // Now we define one record as 1 byte

  // Display the file contents
  // Start with a read of the first 6 bytes. 'count' is set to the
  // actual number read
  ShowMessage('Reading first set of bytes :');
  BlockRead(myFile, byteArray, 6, count);

  // Display the byte values read
  for i := 1 to count do

  // Now read one byte at a time to the end of the file
  ShowMessage('Reading remaining bytes :');
  while not Eof(myFile) do
    BlockRead(myFile, oneByte, 1);   // Read one byte at a time

  // Close the file for the last time
Show full unit code
   Reading first set of bytes :
   Reading remaining bytes :
Delphi Programming Neil Moffatt 2002 - 2020. All rights reserved.  |  Home Page