The readln procedure
Description
The readln procedure reads values from the file
associated with a file variable of
type text
into one or more variables, and then skips to the beginning
of the next line.
Parameters
- The first parameter is optional, and if supplied is a reference to the
file variable associated with the file to read from. If this
parameter is not supplied then the readln procedure reads values from the file
associated with input.
- The other parameters are optional, and if supplied are references to the
variables used to store the values read from the file.
Reading From Text Files
The readln procedure always reads values from a text file (i.e. a file associated
with a file variable of
type text), and the way the values are read is determined
by the type of the
variable used to store the values.
- If a variable of
char type or a subrange
of char type is being used to store the values read from
the text file then a single character is read from the text file and this character is stored
without conversion in the variable.
- If a variable of
string type is being used to store the values read from
the text file then characters are read from the text file and stored in the
variable until either the end of the line is reached or the
number of characters stored in the variable is equal to the maximum number of characters that
can be stored in the variable.
- If a variable of an
integral type or a subrange
of an integral type is being used to store the values read
from the text file then a sequence of characters forming a signed
decimal integer number, possibly with leading
blanks, is read from the file, and converted into the
equivalent value of integer type before being stored in
the variable.
- If a variable of
real type, single type,
or double type is being used to store the values read
from the text file then a sequence of characters forming a signed
real number, possibly with leading
blanks, is read from the file, and converted into the
equivalent value of real type before being stored in the
variable.
Example
For example the program "batch.pas" below is a very primitive batch processor. It reads
lines from a text file using "readln" and send the lines to the command processor to be
executed.
program batch(f, output);
var
f : text;
s : string;
err : integer;
begin
reset(f); (* open input file or standard input file *)
while not eof(f) do
begin
readln(f, s);
writeln('Executing ', s);
err := system(s); (* Pass 's' to the command processor *)
writeln('Error code is ', err)
end
end.
Portability
Operating Systems: All
Standard Pascal: Yes
Standard Pascal does not support reading from text files into
variable of string type.