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. |
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
WriteLn('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
WriteLn('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;
|
Show full unit code |
Reading first set of bytes :
1
2
3
4
5
6
Reading remaining bytes :
7
8
|
|