- 출처: http://docs.sun.com/app/docs/doc/801-7251/6i1iqt7ou?l=ko&a=view
The READ statement reads data from a file or the keyboard to items in the list. If you use this for tapes we recommend the TOPEN() routines instead because they are more reliable.
Syntax
READ( [ UNIT=] u [, [ FMT=] f ] [, IOSTAT= ios ] [, REC= rn ] [, END= s ] [, ERR= s ] ) iolist READ f [, iolist ] READ([UNIT=] u, [NML=] grname [,IOSTAT=ios ] [,END=s ] [,ERR=s ] ) READ [ NML= ] grname
- An alternate to the "UNIT=u, REC=rn" form is as follows. *
READ( u ' rn ... ) iolist
u Unit identifier of the unit connected to the file f Format identifier ios I/O status specifier rn Record number to be read s Statement label for end of file processing iolist List of variables grname Name of a namelist group
- The options can be specified in any order.
Description
Unit Identifier
- u is either an external unit identifier or an internal file identifier.
- An external unit identifier must be one of these:
- Nonnegative integer expression
- Asterisk, identifying stdin, normally connected to the keyboard
- If the optional characters "UNIT=" are omitted from the unit specifier, then u must be the first item in the list of specifiers.
Format Identifier
- f is a format identifier and can be:
- Asterisk (*), indicating list-directed I/O. See Section 5.6 for details.
- Label of a FORMAT statement that appears in the same program unit
- Integer variable name that has been assigned the label of a FORMAT statement that appears in the same program unit
- Character expression or integer array specifying the format string. This is called a runtime format or a variable format. The integer array is nonstandard. *
- If the optional characters "FMT=" are omitted from the format specifier, then f must appear as the second argument for a formatted read, otherwise it must not appear at all.
- Unformatted data transfer from internal files and terminal files is not allowed, hence, f must be present for such files.
- List-directed data transfer from direct-access and internal files is allowed, hence, f can be an asterisk for such files. *
- If a file is connected for formatted I/O, unformatted data transfer is not allowed, and vice versa.
I/O Status Specifier
- ios must be an integer variable or an integer array element.
Record Number
- rn must be a positive integer expression and can be used for direct-access files only. rn can be specified for internal files. *
End-of-File Specifier
- s must be the label of an executable statement in the same program unit in which the READ statement occurs.
- The "END=s" and "REC=rn" specifiers can be present in the same READ statement. *
Error Specifier
- s must be the label of an executable statement in the same program unit in which the READ statement occurs.
Input List
- iolist can be empty or can contain input items and/or implied DO lists. The input items can be any of the following.
- Variables
- Substrings
- Arrays
- Array elements
- Records
- Record fields
- A simple unsubscripted array name specifies all of the elements of the array in memory storage order, with the leftmost subscript increasing more rapidly.
Namelist-directed READ
- The third and fourth forms of the READ statement are used to read the items of the specified namelist group, and grname is the name of the group of variables previously defined in a NAMELIST statement.
- Execution Proceeds as Follows: 1. The file associated with the specified unit is determined. The format, if specified, is established. The file is positioned appropriately prior to the data transfer.
- If the input list is not empty, data is transferred from the file to the corresponding items in the list. The items are processed in order as long as the input list is not exhausted. The next specified item is determined and the value read is transmitted to it. Data editing in formatted READ is done according to the specified format.
- In the third and fourth forms of namelist-directed READ, the items of the specified namelist group are processed according to the rules of namelist-directed input.
- The file is repositioned appropriately after data transfer.
- If ios is specified and no error occurred, it is set to zero. It is set to a positive value, if an error or end of file was encountered.
- If s is specified and end of file was encountered, control is transferred to s.
- If s is specified and an error occurs, control is transferred to s.
- Execution for Keyboard Read There are two forms of READ.
READ f [, iolist ] READ [ NML= ] grname
- They above two forms operate the same way as the others except that reading from the keyboard is implied. Execution has these differences.
- When the input list is exhausted, the cursor is moved to the start of the line following the input. For an empty input list, the cursor is moved to the start of the line following the input.
- If an end-of-line, CR, or NL is reached before the input list is satisfied, input continues from the next line.
- If an end-of-file (Control D) is received before the input list is satisfied, input stops, and unsatisfied items of the input list remain unchanged.
- If u specifies an external unit that is not connected to a file, an implicit OPEN operation is performed which is equivalent to opening the file with the options in the following example.
OPEN( u, FILE='FORT.u', STATUS='OLD', & ACCESS='SEQUENTIAL', FORM=fmt )
- The value of fmt is 'FORMATTED' or 'UNFORMATTED' accordingly, as the read is formatted or unformatted.
- An simple unsubscripted array name specifies all of the elements of the array in memory storage order, with the leftmost subscript increasing more rapidly.
- An attempt to read the record of a direct-access file that has not been written yet causes all items in the input list to become undefined.
- The record number count starts from one.
- Namelist-directed input is permitted on sequential access files only.
Examples
- Example 1: Formatted read, trap I/O errors, EoF, and I/O status.
READ( 1, 2, ERR=8, END=9, IOSTAT=N ) X, Y ... 8 WRITE( *, * ) 'I/O error # ', N, ', on 1' STOP 9 WRITE( *, * ) 'EoF on 1' RETURN END
- Example 2: Direct, unformatted read, trap I/O errors, and I/O status.
... READ( 1, REC=3, IOSTAT=N, ERR=8 ) V ... 4 CONTINUE RETURN 8 WRITE( *, * ) 'I/O error # ', N, ', on 1' END
- Example 3: List-directed read from keyboard.
READ( *, * ) A, V or READ *, A, V
- Example 4: Formatted read from an internal file.
CHARACTER CA*16 / 'abcdefghijklmnop' /, L*8, R*8 READ( CA, 1 ) L, R 1 FORMAT( 2 A8 )
- Example 5: Read an entire array.
DIMENSION V(5) READ( 3, '(5F4.1)') V
- Example 6: Namelist-directed read.
CHARACTER SAMPLE*16 LOGICAL NEW*4 REAL DELTA*4 NAMELIST /G/ SAMPLE, NEW, DELTA ... READ( 1, G ) or READ( UNIT=1, NML=G ) or READ( 1, NML=G )
태그 : FORTRAN90




덧글