Read

From Free Pascal wiki
Jump to navigationJump to search

Read is one of the standard procedures in the Pascal programming language. It is used to accept input from a file. The format is

Read ([filename],variable1 [, variable2 ...]);

Where

  • [filename] is an optional parameter, indicating the file variable to read data from
  • variable1 is the first variable to read data into
  • [, variable2] is an optional second variable to read more data into
  • ... indicates more variables may be added

The read procedure checks the type of variables which have been passed to it and accepts input from the specified file variable and places it into that variable.

If no file variable is specified, the file variable input is used.

If the variable is a character, an array of characters, or a string, the characters will be read up to the size limit of the variable.

If the variable is a numeric variable such as an integer, or real, the read procedure will ignore any leading blanks, and will read in characters until an invalid character for the type of variable is found, and it will then stop reading for that variable. For example, for an integer, it will stop reading if a decimal point or any other non-number is found in the input stream.

For a real variable, a decimal point and the letter "E" followed by an optional + or -, and an exponent, may be used.

If the first non-blank which is to be read by a numeric variable is not a +, - or a digit, a run-time error occurs.

If eof occurs on the file variable passed to the read procedure, a run-time error occurs.

The Read procedure will continue reading from the file variable until the amount of data requested has been satisfied, or an eof is detected. The read procedure generally stops when an eol is detected. The readln procedure is used to absorb the eol as part of the text stream.