DelphiBasics
ReadLn
Procedure
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...};
Description
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).
Notes
ReadLn does not buffer records, so performance is degraded. BlockRead is more efficient, but is geared to writing to binary files.
Related commands
AssignFileAssigns a file handle to a binary or text file
BlockReadReads a block of data records from an untyped binary file
BlockWriteWrites a block of data records to an untyped binary file
EofReturns true if a file opened with Reset is at the end
FileDefines a typed or untyped file
ReadRead data from a binary or text file
ReadLnRead a complete line of data from a text file
ResetOpen a text file for reading, or binary file for read/write
TextFileDeclares a file type for storing lines of text
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 : Reading each text line as a set of 4 numbers
var
  myFile : TextFile;
  n1, n2, n3 : Integer;

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

  // 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
  CloseFile(myFile);

  // Reopen the file for reading
  Reset(myFile);

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

  // Close the file for the last time
  CloseFile(myFile);
end;
Show full unit code
  1 2 3
  5 6 7
  8 9 0
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page