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 |
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 |
|
|
|
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
|
|