Read a complete line of data from a text file System unit
1  procedure ReadLn ( var Variable1 {,var Variable2...} ) ;
2  procedure ReadLn ( var FileHandle : TextFile; var Variable1 {,var Variable2...} ) ;
The ReadLn procedure reads a complete line of data from a text file or to the console.
Version 1
Is used to read from the console.
Version 2
Is used to read a line of text from a text file with the given FileHandle.
You must use AssignFile to assign a file to the FileHandle and open the file with Reset before using ReadLn.
The current file text line is parsed into the given variable values.
After parsing, any remaining text in the current file line is ignored, and the file positioned to the next line (or Eof set true if none left).
ReadLn does not buffer records, so performance is degraded. BlockRead is more efficient, but is geared to writing to binary files.
Related commands
AssignFile Assigns a file handle to a binary or text file
BlockRead Reads a block of data records from an untyped binary file
BlockWrite Writes a block of data records to an untyped binary file
Eof Returns true if a file opened with Reset is at the end
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
TextFile Declares a file type for storing lines of text
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 each text line as a set of 4 numbers
// 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;
  // The System unit does not need to be defined
  Forms, Dialogs;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
Form1: TForm1;
{$R *.dfm} // Include form definitions
procedure TForm1.FormCreate(Sender: TObject);

  myFile : TextFile;
  n1, n2, n3 : Integer;

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

  // Write numbers in a string
  WriteLn(myFile, '1 2 3 4');   // Note that the 4 will be ignored

  // Write numbers as separate parameters
  WriteLn(myFile, 5, ' ', 6, ' ', 7);

  // Write numbers as separate parameters
  WriteLn(myFile, '8 9');       // Missing numbers will be seen as 0

  // Close the file

  // Reopen the file for reading

  // Display the file contents
  while not Eof(myFile) do
    ReadLn(myFile, n1, n2, n3);
    ShowMessage(IntToStr(n1)+' '+
                IntToStr(n2)+' '+

  // Close the file for the last time
Hide full unit code
   1 2 3
   5 6 7
   8 9 0
Delphi Programming Neil Moffatt 2002 - 2020. All rights reserved.  |  Home Page