DelphiBasics
Eof
Function
Returns true if a file opened with Reset is at the end System unit
 function Eof(var FileHandle TextFile);
Description
The Eof function returns true if the file given by FileHandle is at the end.
 
The file must have been assigned, and opened with Reset.
Notes
Warning after reading the last line of a file, Eof will be true, even though data has been read successfully.

So use Eof before reading, to see if reading is required.
Related commands
BlockReadReads a block of data records from an untyped binary file
EolnReturns true if the current text file is pointing at a line end
ReadRead data from a binary or text file
ReadLnRead a complete line of data from a text file
SeekEofSkip to the end of the current line or file
SeekEolnSkip to the end of the current line or file
 Download this web site as a Windows program.




 
Example code : Reading to the end of a text file
var
  myFile : TextFile;
  text   : string;

begin
  // Try to open the Test.txt file for writing to
  AssignFile(myFile, 'Test.txt');
  ReWrite(myFile);

  // Write a couple of well known words to this file
  WriteLn(myFile, 'Hello');
  WriteLn(myFile, 'World');

  // Close the file
  CloseFile(myFile);

  // Reopen the file in read only mode
  Reset(myFile);

  // Display the file contents
  while not Eof(myFile) do
  begin
    ReadLn(myFile, text);
    ShowMessage(text);
  end;

  // Close the file for the last time
  CloseFile(myFile);
end;
Show full unit code
  Hello
  World
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page