SmartPascal
BlockRead
Procedure
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} ) ;
Description
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
 
Example code : Reading data from a binary file one byte at a time.
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  // The System unit does not need to be defined
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  myFile    : File;
  byteArray : array[1..8] of byte;
  oneByte   : byte;
  i, count  : Integer;

begin
  // 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
  CloseFile(myFile);

  // 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
    ShowMessage(IntToStr(byteArray[i]));

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

  // Close the file for the last time
  CloseFile(myFile);
end;
 
end.
Hide full unit code
   Reading first set of bytes :
   1
   2
   3
   4
   5
   6
   Reading remaining bytes :
   7
   8