Must Use readln to Read End of Text Line

Product Version(s): 3.20 3.3x
Operating System:   MS-DOS
Flags: ENDUSER |
Last Modified:  3-OCT-1988    ArticleIdent: Q10381

Question:

The file READ.PAS is an example of a program that uses a read
statement to input the value of an lstring variable. The program loops
indefinitely after the first value is read and written. Replacing the
read with a readln causes the program to run as I would expect. Why
does this occur?

Is the read statement unable to read past the end of a line of text
when reading an lstring value? This problem also occurs with string
variables. The following READ.PAS program is an example:

   program read_lstring(input,output);
   VAR s : lstring(6);
   begin
   repeat
   write('enter string > ');
   read(s);
   writeln(s)
   until (s = 'bye');
   end.

Response:

You must use readln to read past the end-of-line marker for a text
file. The files INPUT and OUTPUT, declared in the program statement,
are text files.

INPUT and OUTPUT are wholly distinct as far as Pascal is concerned.
Thus, the fact that an intervening writeln (or two) may have advanced
the line on the console device has no effect on the reads.

All reads will take their data from the 80 character INPUT file buffer
until there is an intervening readln. Because read(s), where "s" is an
lstring variable, is satisfied with null input, you can have
infinitely many consecutive read(s)s.

Note: using readln (and writeln) only applies to text files. Other
files defined by the type statement "file of <type>;" do not have
end-of-line markers to be read/written by readln/writeln.

