Read

Statement Type:

The READ statement is an executable statement.

Statement Purpose:

The READ statement is used to pick up information for input from external sources. These sources could be input from the keyboard, computer file or a magnetic tape. It can also be used to convert numbers represented within CHARACTER variables to a machine numerical representation (REAL, INTEGER, etc.).
Required Form:

      READ( input_arguments) variable_list
or
      READ format, variable_list

Required Arguments:

UNIT =

This tells the computer from which source to aquire the desired input. If an integer (constant or variable) is placed to the right of the equals sign, that should be the number of a file that was OPENED earlier in the program. If there is a * after the equals sign this tells the compiler to get the information from the default input unit ( in most cases the keyboard ). The literal "unit=" is usually not included, and the information provided by and integer constant or variable immediatly after the left parenthesis.

FMT =

This is required for formatted input. It is not included when reading from an unformatted (binary) file. Three things can appear after this equals sign. First, is a format line label number. This is the label number a FORMAT statement that the computer uses to get information on the way in which the data should be read. The second thing that can go after the equals sign is a *. If this appears, it just tells the computer that it should read the data in the fashion that it feels is most appriate for the variable list ( for instance if it is reading in information for a character variable, it will read whatever is typed at the keyboard as a character ). The last thing that can be seen after the equals sign is a set of edit descriptors placed between a set of single quotes and a set of parenthesis. As with the UNIT argument, the literal "FMT=" need not be included. The contents of the second field in the argument list is interpreted as the format information.

Optional Arguments:

IOSTAT=

This specifies an integer variable that will contain an error condition number. A value of zero means no error. A negative number means the end of the file was reached while a positive number means some serious problems were detected. The exact meaning of a given positive number is system dependent so to find out what it means, you'll have to reach for a manual on your specific system.

ERR=

This argument will have a label number placed after the equal sign. This label number is were the program will branch if an error is detected.

ADVANCE=

Under normal circumstances, after a read is done it will position the file at the next record line even if it didn't read all the information on the previous line. To keep the position of the file after the last variable read, type in the 'NO' option. By default, advance will equal 'YES'.

REC=

This is used in direct access files only. An integer constant or variable is placed after the equals sign to denote which record to read.

END=

This will have a label number after it to which the program will branch, if it encounters the end of the file while reading.

EOR=

This is another branching option with the READ statement. A labe number goes after the equals sign for the program to branch to in case it tries to read to many variables off of one line. This must be accompanied by the ADVANCE ='NO' option.

Examples:

To get a value from the keyboard for the real variable x :
		read *, x
or
		read (unit=*,fmt=*)
To get input for the character variable string:
		read (*,'(a)') string
or
		read (*,100) string
	 100 format (a)
To input the string of numbers 3.6, 4.5, 8.9, and 9.9 from a file opened as unit 12 into elements 6 - 9 off array new.
		read (12,'(4(f3.1,1x))') new(6:9)
or
		read (unit=12,fmt=101) new(6:9)
	 101 format (4(f3.1,1x))
The following statement takes the character representation of two decimal digits from CHARARTER variable "num" and puts the result into the INTEGER variable "inum".
          read( num, '(i2)') inum
Finally, the statement
		read(11,1001, err=700,iostat=ioerr,rec=93) x,y,z
will read values for x, y, and z from a direct access file assigned to unit 11, according to FORMAT number 1001 (this is unusual since direct access files are normally unformatted). If an error occurs, the program will go to label 700, where the value of variable "ioerr" could be printed, or acted on with logic specific to the current computer

Related Topics, Lectures and Examples:

write statement

open statement

close statement

format statement

lecture twenty two

examples: noadv.f and charvar.f

Up one level / Home


Written by Jason Wehr : jcw142@psu.edu and Maintained by John Mahaffy : jhm@cac.psu.edu