Writes a block of data records to an untyped binary file System unit
 procedure BlockWrite(var FileHandle File; var Buffer; RecordCount Integer {; var RecordsWritten Integer});
The BlockWrite procedure is used to write to RecordCount data records from Buffer to an untyped binary file given by the FileHandle.
The file must have been assigned using AssignFile and opened with Reset or ReWrite.
The Reset and ReWrite routines by default will open the file with a record size of 128 bytes. This can be overriden in these routines to a value more useful to yourself (see example).
Data is taken from the Buffer (normally a string or byte array) and written to 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 buffer to do this.
The actual number of records written is stored in the optional RecordsWritten variable, if provided. It may be les than RecordCount if, for example, the hard disk was full.
Related commands
BlockReadReads a block of data records from an untyped binary file
FileDefines a typed or untyped file
ReadRead data from a binary or text file
ReadLnRead a complete line of data from a text file
WriteWrite data to a binary or text file
WriteLnWrite a complete line of data to a text file
 Download this web site as a Windows program.

Example code : Writing three 4-byte records in 2 block write to a binary file.
  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

  // Fill out the data array with different data
  for i := 1 to 4 do
    byteArray[i] := i*i;              // Value : 1, 4, 9, 16

  // Write only the first 4 items from the data array to the file
  BlockWrite(myFile, byteArray, 1);  // Write 1 record of 4 bytes

  // Close the file

  // Reopen the file for reading only
  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 and display 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 . All rights reserved.  |  Home Page