This chapter describes the types of input and output available and provides examples of input, output and format statements. There are four types of input/output usd to transfer data to or from files: unformatted, formatted, list directed, and namelist.
Files may contain one of two types of records, fixed length records or variable length records. To specify the size of the fixed length records in a file, use the RECL specifier with the OPEN statement. RECL sets the record length in bytes .[1]RECL can only be used when access is direct.
A record in a variable length formatted file is terminated with \n 2. A record in a variable length unformatted file is preceded and followed by a word indicating the length of the record.
OPEN (list)where list contains a unit specifier of the form:
[UNIT=] uwhere u, an integer, is the external unit specifier.
In addition list may contain one of each of the specifiers shown in Table 6-1.
Any file opened for direct access must be via fixed length records.
In the following example a new file, book.dat, is created and connected to unit 12 for direct formatted input/output with a record length of 98 characters. Numeric values will have blanks ignored and the variable E1 will be assigned some positive value if an error condition exists when the OPEN statement is executed; execution will then continue with the statement labeled 20. If no error condition pertains, E1 is assigned the value 0 and execution continues with the statement following the OPEN statement.
OPEN(12,IOSTAT=E1,ERR=20,FILE='book.dat',BLANK='NULL', +ACCESS='DIRECT',RECL=98,FORM='FORMATTED',STATUS='NEW')
Provided the file is still in existence it may be reconnected to the same or a different unit after the execution of a CLOSE statement. An implicit CLOSE is executed when a program stops.
The CLOSE statement terminates the connection of the specified file to a unit.
CLOSE ([UNIT=] u [,IOSTAT=ios] [,ERR= errs ]CLOSE takes the status values IOSTAT, ERR, and STATUS, similar to those described in Table 6-1 OPEN Specifiers. In addition, CLOSE allows the DISPOSE or DISP specifier which can take a status value sta which is a character string, where case is insignificant, specifying the file status (the same keywords are used for the DISP and DISPOSE status). Status can be can be KEEP or DELETE. KEEP cannot be specified for a file whose dispose status is SCRATCH. When KEEP is specified (for a file that exists) the file continues to exist after the CLOSE statement, conversely DELETE deletes the file after the CLOSE statement. The default value is KEEP unless the file status is SCRATCH.
[,STATUS= sta] [,DISPOSE= sta] [,DISP= sta])
Table 6-1 OPEN Specifiers
Specifier Description ACCESS= acc Where acc is a character string specifying the access method for file connection as DIRECT (random access) or SEQUENTIAL. The default is SEQUENTIAL. ACTION= act Where act is a character string specifying the allowed actions for the file and is one of READ, WRITE, or READWRITE. BLANK= blnk Where blnk is a character string which takes the value NULL or ZERO: NULL causes all blank characters in numeric formatted input fields to be ignored with the exception of an all blank field which has a value of zero. ZERO causes all blanks other than leading blanks to be treated as zeros. The default is NULL. This specifier must only be used when a file is connected for formatted input/output. DELIM=del Specify the delimiter for character constants written by a list-directed or namelist-formatted statement. The options are APOSTROPHE, QUOTE, and NONE.
Table 6-1 OPEN Specifiers (continued)
Specifier Description ERR= errs An error specifier which takes the form of a statement label of an executable statement in the same program. If an error condition occurs, execution continues with the statement specified by errs.- FILE= fin Where fin is a character string defining the file name to be connected to the specified unit. FORM= fm Where fm is a character string specifying whether the file is being connected for FORMATTED or UNFORMATTED output respectively. The default is FORMATTED. IOSTAT= ios Input/output status specifier where ios is an integer scalar memory reference. If this is included in list, ios becomes defined with 0 if no error exists or a positive integer when there is an error condition.3 PAD=padding Specifies whether or not to use blank padding for input items. The padding values are YES and NO. The value NO requires that the input record and the input list format specification match. POSITION=pos Specifies the position of an opened file. ASIS indicates the file position remains unchanged. REWIND indicates the file is to be rewound, and APPEND indicates the file is to positioned just before an end-of-file record, or at its terminal point. RECL= rl Where rl is an integer which defines the record length in a file connected for direct access and is the number of characters when formatted input/output is specified. This specifier must only be given when a file is connected for direct access. STATUS= sta The file status where sta is a character expression: it can be NEW, OLD, SCRATCH, REPLACE or UNKNOWN. When OLD or NEW is specified a file specifier must be given. SCRATCH must not be used with a named file. The default is UNKNOWN.A unit may be the subject of a CLOSE statement from within any module. If the unit specified does not exist or has no file connected to it the use of the CLOSE statement has no effect. Provided the file is still in existence it may be reconnected to the same or a different unit after the execution of a CLOSE statement. Note that an implicit CLOSE is executed when a program stops.
In the following example the file on UNIT 6 is closed and deleted.
CLOSE(UNIT=6,STATUS='DELETE')
The general form for these data transfer statements is shown in Chapter 3.Refer to Chapter 3 for details on the READ and WRITE and PRINT statements and their valid I/O control specifiers.
OPEN (2, FILE='new.dat', FORM='UNFORMATTED')The unit specified must be an external unit.
After data is transferred, the file is positioned after the last record read or written, if there is no error condition or end-of-file condition set Unformatted data transfer cannot be carried out if the file is connected for formatted input/output.
The following example shows an unformatted input statement:
READ (2, ERR=50) A, BOn output to a file connected for direct access, the output list must not specify more values than can fit into a record. If the values specified do not fill the record the rest of the record is undefined.
On input the following conditions must pertain; the file must be positioned so that the record read is either:
For variable length record formatted input, each newline character is interpreted as a record separator. On output, the I/O system writes a newline at the end of each record. If a program writes a newline itself, the single record containing the newline will appear as two records when read or backspaced over. The maximum allowed length of a record in a variable length record formatted file is 2000 characters.[4]
(iolist,do-var=var1,var2,var3)where the items in iolist are either items permissible in an input/output list or another implied DO list,. The value do-var is an INTEGER, REAL or DOUBLE PRECISION variable and var1, var2 and var3 are arithmetic expressions of type INTEGER, REAL or DOUBLE PRECISION. Generally do-var, var1, var2 and var3 are of type INTEGER. Should iolist occur in an input statement, the do-var cannot be used as an item in iolist. If var3 and the preceding comma are omitted the increment takes the value 1. The list items are specified once for each iteration of the DO loop with the DO-variable being substituted as appropriate.
REAL C(6),D(6) DATA OXO,(C(I),I=7,9),TEMP,(D(J),J=1,2)/4*0.0,3*10.0/In the above example OXO, C(7), C(8) and C(9) are set to 0.0 with TEMP, D(1) and D(2) being set to 10.0. In the next example:
READ *,A,B,(R(I),I=1,4),Shas the same effect as
READ *,A,B,R(1),R(2),R(3),R(4),S
When a format identifier in a formatted input/output statement is a character array name or other character expression, the leftmost characters must be defined with character data that constitute a format specification when the statement is executed. A character format specification is enclosed in parentheses. Blanks may precede the left parenthesis. Character data may follow the right-hand parenthesis and has no effect on the format specification. When a character array name is used as a format identifier, the length of the format specification can exceed the length of the first element of the array; a character array format specification is considered to be an ordered concatenation of all the array elements. When a character array element is used as a format identifier the length must not exceed that of the element used.
The FORMAT statement has the form:
FORMAT (list-of-format-requirements)The list of format requirements can be any of the following, separated by commas:
Each repeatable edit code has a corresponding item in the iolist; however when a list item is of type complex two edit codes of F, E, D or G are required. The edit codes P, X, T, TL, TR, S, SP, SS, H, BN, BZ, /, : and apostrophe act directly on the record and have no corresponding item in the input/output list.
The file is positioned after the last character read or written when the edit codes I, F, E, D, G, L, A, H or apostrophe are processed. If the specified unit is a printer then the first character of the record is used to control the vertical spacing as shown in Table 6-2:
Table 6-2 Format Character Controls for a Printer
Character Vertical Spacing Blank One line 0 Two lines 1 To first line on next page + No advance
On output, if l is the length of the character item and w is the field width, then the following rules apply:
If w > l w - l blanks before the character.
If w < l leftmost w characters.
On input, if l is the length of the character I/O item and w is the field width, then the following rules apply:
If w > l rightmost l characters from the input filed.
If w < l leftmost w characters from the input filed and followed by l - w blanks.
You can also use the A format specifier to process data types other than CHARACTER. For types other than CHARACTER, the number of characters supplied for input/output will equal the size in bytes of the data allocated to the data type. For example, an INTEGER*4 value is represented with 4 characters and a LOGICAL*2 is represented with 2 characters.
The following shows a simple example that reads two CHARACTER arrays from the file data.src:
CHARACTER STR1*8, STR2*12
OPEN(2, FILE='data.src')
READ(2, 10) STR1, STR2
10 FORMAT ( A8, A12 )
Bw[.m]where w specifies the field width and m indicates minimum field width on output.
On input, the external field to be input must contain (unsigned) binary characters only (0 or 1). An all blank field is treated as a value of zero. If the value of the external field exceeds the range of the corresponding list element, an error occurs.
On output, the B field descriptor transfers the binary valuesof the corresponding I/O list element, right-justified, to an external field that is w characters long. If the value to be transmitted does not fill the field, leading spaces are inserted; if the value is too large for the field, the entire field is filled with asterisks. If m is present, the external field consists of at least m digits, and is zero-filled on the left if necessary. Note that if m is zero, and the internal representation is zero, the external field is blank-filled.
Dw.dwhere w is the field width and d the number of digits in the fractional part.
For example:
DOUBLE PRECISION VAL1produces the following:
VAL1 = 141.8835
WRITE( *, 20) VAL1
20 FORMAT ( D10.4 )
0.1418D+03
Ew.dw is the field width, d the number of digits in the fractional part and e the number of digits to be printed in the exponent part.
Ew.dEe
For input the same conditions apply as for F editing. For output the scale factor controls the decimal normalization as in D above.
ENw.dw is the field width, d the number of digits in the fractional part and e the number of digits to be printed in the exponent part.
ENw.dEe
On output, the number is in engineering notation where the exponent is divisible by 3 and the absolute value of the significand is 1000 > |significand | >=1. This format is the same as the E format descriptor, except for restrictions on the size of the exponent and the significand.
ESw.dw is the field width, d the number of digits in the fractional part and e the number of digits to be printed in the exponent part.
ESw.dEe
For output the scale factor controls the decimal normalization as in D above.
On output, the number is presented in scientific notation, with the absolute value of the significan is 10> | significand | >= 1.
Fw.dw is the field width and d is the number of digits in the fractional part.
On input if the field does not contain a decimal digit or an exponent, righthand d digits, with leading zeros, are interpreted as being the fractional part.
On output a leading zero is only produced to the left of the decimal point if the value is less than one.
Gw.d Gw.dEeThe specifier transfers real values; it acts like the F format control on input and depending on the value's magnitude, like E or F on output. The magnitude of the data determines the output format. For details on the actual format used, based on the magnitude, refer to Section 13.5.9.2.3 "G Editing", in the ANSI FORTRAN Standard.
Iw Iw.mwhere w is the field width and m is the minimum filed width on output, including leading zeros. If present, m must not exceed width w.
On input, the external field to be input must contain (unsigned) decimal characters only. An all blank field is treated as a value of zero. If the value of the external field exceeds the range of the corresponding list element, an error occurs.
On output, the I format descriptor transfers the decimal values of the corresponding I/O list element, right-justified, to an external field that is w characters long. If the value to be transmitted does not fill the field, leading spaces are inserted; if the value is too large for the field, the entire field is filled with asterisks. If m is present, the external field consists of at least m digits, and is zero-filled on the left if necessary. Note that if m is zero, and the internal representation is zero, the external field is blank-filled.
LwThe L format control transfers logical data of field width w. On input the list item will become defined with a logical value; the field consists of optional blanks, followed by an optional decimal point followed by T or F. Also, the values .TRUE. or .FALSE. may appear in the input field
The output field consists of w-1 blanks followed by T or F as appropriate.
To write an apostrophe (or quote) use two consecutive apostrophes (or quotes).
For example:
WRITE ( *, 101) 101 FORMAT ( 'Print an apostrophe '' and end.')Produces:
Print an apostrophe ' and end.Similarly, you can use quotes, for example:
WRITE ( *, 102) 102 FORMAT ( "Print a line with a "" and end.")Produces:
Print a line with a " and end.
BZ causes all blanks except leading blanks in numeric input to be replaced by zeros. Only input statements and I, F, E, D and G editing are affected.
The basic form of this format specification is:
nHc1cn...where n is the number of characters to print and c1 through cn are the characters to print.
Ow[.m] and Zw[.m]where w specifies the field width and m indicates minimum field width on output.
On input, the external field to be input must contain (unsigned) octal or hexadecimal characters only. An all blank field is treated as a value of zero. If the value of the external field exceeds the range of the corresponding list element, an error occurs.
On output, the O and Z field descriptors transfer the octal and hexadecimal values, respectively, of the corresponding I/O list element, right-justified, to an external field that is w characters long. If the value to be transmitted does not fill the field, leading spaces are inserted; if the value is too large for the field, the entire field is filled with asterisks. If m is present, the external field consists of at least m digits, and is zero-filled on the left if necessary. Note that if m is zero, and the internal representation is zero, the external field is blank-filled.
kPThe P format specifier is the scale factor format which is applied as follows.
DIMENSION A(6)produces:
DO 10 I = 1,6
10 A(I) = 25.
TYPE 100,A
100 FORMAT(' ',F8.2,2PF8.2,F8.2)
25.00 2500.00 2500.00 2500.00 2500.00 2500.00Note that the effect of the scale factor continues until another scale factor is used.
SP forces the processor to write a plus in any position where an optional plus is found in numeric output fields, this only affects output statements.
SS stops the processor from writing a plus in any position where an optional plus is found in numeric output fields, this only affects output statements.
Tnthis specifies that the nth value is to be written to or from a record.
The TL form specifies the relative position to the left of the data to be read or written.
TLnThis specifies that the nth character to the left of the current position is to be written to or from the record. If the current position is less than or equal to n the transmission will begin at position one of the record.
The TR form specifies the relative position to the right of the data to be read or written.
TRnSpecifies that the nth character to the right of the current position is to be written to or from the record.
The X control specifies a number of characters to skip forward.
nXSpecifies that the next character to be written to or from is n characters forward from the current position.
The following example uses the X format specifier.
NPAGE = 19 WRITE ( 6, 90) NPAGE 90 FORMAT('1PAGE NUMBER ,I2, 16X, 'SALES REPORT, Cont.')produces:
PAGE NUMBER 19 SALES REPORT, Cont.The following example shows use of the T format specifier.
PRINT 25produces:
25 FORMAT (T41,'COLUMN 2',T21,'COLUMN 1')
COLUMN 1 COLUMN 2
Ow[.m] and Zw[.m]where w specifies the field width and m indicates minimum field width on output.
On input, the external field to be input must contain (unsigned) octal or hexadecimal characters only. An all blank field is treated as a value of zero. If the value of the external field exceeds the range of the corresponding list element, an error occurs.
On output, the O and Z field descriptors transfer the octal and hexadecimal values, respectively, of the corresponding I/O list element, right-justified, to an external field that is w characters long. If the value to be transmitted does not fill the field, leading spaces are inserted; if the value is too large for the field, the entire field is filled with asterisks. If m is present, the external field consists of at least m digits, and is zero-filled on the left if necessary. Note that if m is zero, and the internal representation is zero, the external field is blank-filled.
On input from a file connected for sequential access the rest of the current record is skipped and the file positioned at the start of the next record.
On output a new record is created which becomes the last and current record. For an internal file, connected for direct access the record is filled with blank characters. If a direct access file, the record number is increased by one and the file is positioned at the start of the record.
Multiple slashes are permitted, thus multiple records are skipped.
The form of the $ field descriptor is:
$
For nonadvancing input/output, use the ADVANCE='NO' specifier. Two other specifiers apply to nonadvancing, EOR which applies when end of record is detected and SIZE which returns the number of characters read.
READ( 1, * ) VAL1, VAL2
Table 6-3 List Directed Input Values
Input List Type Form Integer A numeric input field. Real A numeric input field suitable for F editing with no fractional part unless a decimal point is used. Double Same as for real. precision Complex An ordered pair of numbers contained within parentheses as shown (real part, imaginary part). Logical A logical field without any slashes or commas. Character A non-empty character string within apostrophes. A character constant can be continued on as many records as required. Blanks, slashes and commas can be used.A null value has no effect on the definition status of the corresponding iolist item. A null value cannot represent just one part of a complex constant but may represent the entire complex constant. A slash encountered as a value separator stops the execution of that input statement after the assignment of the previous value. If there are further items in the list they are treated as if they are null values.
Commas may be used to separate the input values. If there are consecutive commas, or the if the first non-blank character of a record is a comma, the input value is a null value. Input values may also be repeated.
In the following example of list-directed formatting, assume that
A= -1.5and all other variables are undefined. When the statement below reads in the list from the input file:
K= 125
READ * I, J, X, Y, Z, A, C, Kwhere the file contains the following record:
10,-14,25.2,-76,313,,29/The variables are assigned the following values by the list-directed input/output mechanism:
I=10 J=-14 X=25.2 Y=-76.0 Z=313.0 A=-1.5 C=29 K=125.Note that the value for A does not change because the input record is null (consecutive commas). No input is read for K, so it assumes null and K retains its previous value ( the / terminates the input).
Table 6-4 Default List Directed Output Formatting
Data Type Default Formatting BYTE I5 INTEGER*2 I7 INTEGER*4 I12 LOGICAL*1 I5 (L25 ) LOGICAL*2 L2 LOGICAL*4 L2 REAL*4 G15.7e2 REAL*8 G25.16e3 COMPLEX*8 (G15.7e2, G15.7e2) COMPLEX*16 (G25.16e3, G25.16e3) CHAR *n AnThe length of a record is less than 80 characters; if the output of an item would cause the length to exceed 80 characters, a new record is created.
The general form of a namelist statement is:
NAMELIST /group-name/ namelist [[,] /group-name/ namelist ]...
READ ([unit=] u, [NML=] namelist-group [,control-information])One or more records are processed which define the input for items in the namelist group.
The records are logically viewed as follows:
$group-name item=value [,item=value].... $ [END]The following rules describe these input records:
WRITE ([unit=] u, [NML=] namelist-group [,control-information])The records output are logically viewed as follows:
$group-name item = value $ [END]The following rules describe these output records: