RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.ReadLn Function

Reads a line of text from a file.

Pascal
procedure ReadLn(V1); overload;
procedure ReadLn(V1; Vn); overload;
procedure ReadLn(var F: Text; V1); overload;
procedure ReadLn(var F: Text; V1; Vn); overload;
C++
ReadLn( V1);
ReadLn( V1,  Vn);
ReadLn(Text F,  V1);
ReadLn(Text F,  V1,  Vn);

In Delphi code, the ReadLn procedure reads a line of text and then skips to the next line of the file. 

The syntax shown here for ReadLn demonstrates that the procedure can take a variable number of arguments. 

ReadLn(F) with no parameters causes the current file position to advance to the beginning of the next line if there is one; otherwise, it goes to the end of the file. 

If F is omitted, the global variable Input is used, which accesses the processes standard input file. Use of Input in GUI applications raises special issues.

Note: {$I+} handles runtime errors using exceptions. When using {$I-}, use IOResult to check for I/O errors.
 

Delphi Examples: 

 

{
This example assumes you are compiling as a console
application.  Notice how the form deactivates when the button
click procedure exits.
}

{$APPTYPE CONSOLE}

procedure TForm1.Button1Click(Sender: TObject);
var
  s : string;
begin
  Write('Enter a line of text: ');
  Readln(s);
  Writeln('You typed: ',s);
  Writeln('Hit <Enter> to exit');
  Readln;
end;
{
Use a TOpenDialog to select a file.  Place the first line of
the file into a TEdit.  Click on the text edit to open the
open dialog box. 
}
procedure TForm1.Edit1Click(Sender: TObject);
var
  F: TextFile;
  S: string;
begin
  if OpenDialog1.Execute then            { Display Open dialog box }
  begin
    AssignFile(F, OpenDialog1.FileName); { File selected in dialog }
    Reset(F);
    Readln(F, S);                        { Read first line of file }
    Edit1.Text := S;                     { Put string in a TEdit control }
    CloseFile(F);
  end;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!