community.borland.com

Article #15894: READ & READLN - DIFFERENCES BETWEEN 3.0 AND 4.0+

 Technical Notes Database

TN894D.txt   READ & READLN - DIFFERENCES BETWEEN 3.0 AND 4.0+
Category   :Pascal
Platform    :All
Product    :Turbo Pascal  3.0+

Description:
Q. What are the changes between Read and Readln of 3.0 and 4.0+?
A. The problem which you have encountered is due to a
   modification of the READ procedure in Turbo Pascal version
   4.0+.  In version 4.0+, the READ procedure was altered to
   allow the creation of text file device drivers.  Also, the
   Read procedure handles keyboard input in the same manner as if
   reading from a disk file.  This change has the following
   effects:

     1) The carriage return and line feed character combination
        generated when the Return key is pressed, is no longer
        cleared from the file input buffer.  However, the cursor
        still advances to the beginning of the next line.

     2) A Read(NumericVar) call no longer defaults the
        NumericVar's  value.  In version 3.0, a simple press of
        the Return key  caused the program to retain the value of
        the NumericVar  and processing continued.  In version
        4.0, a non white-space character input is required.
        Simply pressing the  Return key will cause the cursor to
        advance to the next line and continue to wait for input.

     3) Since the carriage return and line feed combination no
        longer are handled, they remain in the file input  buffer
        until the next Read call.  If the next call is of the
        type Read(CharacterVar), CharacterVar will return the
        carriage return character, ASCII 13. A subsequent
        Read(CharacterVar) call will return the line feed
        character, ASCII 10.  For this reason a Read(Ch);  call
        may appear to have no effect, if it follows Read
        procedure call.

     4) A Read(StringVar) will have a different effect. With  the
        ASCII 13, carriage return and 10, line feed  characters
        as the next to be read in the keyboard  buffer, the EOLN
        function returns true.  When reading  from a disk file a
        Read(FileVar, StringVar) returns a  null string in
        StringVar. The same is true with keyboard input. For
        example the following code will create an  infinite loop:

          Read(AnyVar);
          Repeat
            Read(StringVar);
          Until StringVar <> '';

   I suggest that you replace all Read(from standard input) to
   Readln calls.  Many developers have resolved this  difference
   by implementing ReadChar, ReadString, ReadInt, and ReadReal
   procedures based on the ReadKey function.


Reference:


7/16/98 4:35:37 PM
 

Last Modified: 01-SEP-99