We now briefly look at both of these parts.
READ(IDEV,IFORM) variable listand the standard output command is:
WRITE(IDEV,IFORM) variable list
By default, both commands start their actions on a new line whenever executed. In other words, three WRITE commands normally correspond to at least three output lines. Each invocation of a READ commands starts at the beginning of a new line in the input file, ignoring whatever may have remained on the previous input line.
The variable list is a list of variables separated by commas. If no variable values are needed (e.g., if a WRITE statement is to print out a constant character string), then the variable list is omitted. Literal character strings may be included in this list of variables if they are enclosed in either single or double quotes (FORTRAN-77 only permits single quotes). (Such literal strings may also be included as part of the FORMAT statement.)
IDEV refers to the "device" number. Originally, this number was required to distinguish between different input or output devices (such as paper tape, "IBM"-cards, magnetic tape) on single-user system. Today, this number provides an easy way to distinguish multiple input or output files or devices being used in the same program. IDEV may be an integer constant, or it can be an integer variable (whose value is assigned by a standard arithmetic assignment statement). Values vary from installation to installation, but now 5 is commonly associated with keyboard input (i.e., standard input), and 6 with video display output (i.e., standard output).
Most systems allow the user to specify associations between "device" numbers and stored files, requiring a user to predetermine this association when coding the program (see OPEN, CLOSE commands described below in section 3.12). Some systems, however, allow this association to be done at run or compile time. (For example, compilers run on many Unix systems, will automatically associate device numbers other than 5, 6, or 7, with files names ftnnn where nn corresponds to the device number used. For example, on many Unix systems, WRITE(23,101) A,B,C would write the values of A, B and C to file "ftn23" automatically, without any sort of additional information required from the programmer. Similarly READ(1,102) A would read from file "ftn01" automatically.)
Most systems will also allow the user to use an asterisk in place of any IDEV value, e.g., WRITE(*,101). The asterisk indicates the standard input or output unit designated by the system, which is commonly the keyboard and the monitor screen.
IFORM normally refers to the FORMAT statement number which will be discussed in sections below. IFORM should normally be an integer constant, but it may be an integer variable whose value (a statement label) is assigned by means of ASSIGN statement.
For informal code, one may substitute an asterisk for the IFORM number, e.g., WRITE(6,*) A. The asterisk provides "format-free" (i.e., default) input or output. "Format-free I/O" will be discussed in more detail in the next section.
IFORM may also be replaced by a character variable or character constant string containing all the formatting information usually contained in the FORMAT statement. This approach has the advantage of incorporating into one physical statement the same information traditionally contained in two statement while also eliminating dependence on statement labels. (The use of statement labels is discouraged by contemporary programming stylists).
The following are simple examples for READ and WRITE statements:
READ(5,100) A,B,C,D WRITE(6,101) A,B,C WRITE(6,102) WRITE(*,*) A, BCOMMENTS:
For example,
WRITE(6,*) I,A,B,Jor
READ(5,*) A,B,Jor
WRITE(6,*) ' I =',I
100 FORMAT( descriptor list )
Within the parentheses of the FORMAT statement is a list of "edit descriptors" (also called "field descriptors") separated by commas. The descriptors are used to describe the input/output line space by space (i.e., column by column). Each FORMAT statement describes at least one line. Although FORMAT statements can be located anywhere in a program segment, there are, however, two general styles:
NOTE: a FORMAT statement referenced by a READ or WRITE statement must be in the same program segment as the corresponding READ or WRITE statement -- it cannot be in a separate subprogram. Note that the same FORMAT statement label number may be re-used by statements in other subprogram sections, since statement labels are also local in Fortran.
General Note Regarding Numeric Edit Descriptors: If the width prescribed for a field is too narrow to fit the number that is supposed to be printed, Fortran will not expand the field to fit the number. In this situation, most Fortran compilers will fill the output field with asterisks (and give a run-time error).
In Integer:
n corresponds to the number of spaces needed.In.m Integer:
n corresponds to the number of spaces needed.
m corresponds to the minimum number of digits printed.
This descriptor forces initial zeroes if the actual number has fewer than m digits. In practice, it is rarely used, the previous one being used instead.Fw.d Real number in Fixed form:
w corresponds to the total number of spaces needed.
d corresponds to the number of digits to the right of the decimal point.Ew.d Real number in Exponential form:
w corresponds to the total number of spaces needed.
d corresponds to the number of digits to the right of the decimal point.
An E descriptor (for output) needs one space for the letter E, three spaces for the exponent and its sign, three more spaces for the decimal point, an initial zero before the decimal point, and a possible minus sign. Thus, in general (for output), w should be greater than or equal to d + 7.Ew.dEe Real number in Exponential form:
w and d are the same as with the previous descriptor.
e corresponds to the number of digits in the exponent.
In this case, w must be greater than or equal to d+e+5. In practice, this form is rarely used.nX Spacing:
The number n before the X indicates how many blanks should be left (on output), or spaces skipped (on input).'string'
or
"string"
Literal string:
The string contained between the single or double quotes will be printed out literally.
Note: in FORTRAN-77 only the single quote (normally) is permitted. To include a single quote within a string, one must use two single quotes next to each other./ End of Record separator:
On Output: the slash will force a new line and print any data associated with subsequent descriptors in the FORMAT statement to be printed on next line (assuming the new carriage control character does not force some other action).
On Input: the slash will cause the rest of the input line to be ignored and start reading data from the next input line. Slashes can be used instead of commas, and more than one can be indicated together.
There is an older version of an edit descriptor for literal strings which may still be used in FORTRAN-77 and Fortran-90. (It is often seen in older codes.) It was, however, removed from the definition of Fortran-95 (and Fortran-2003). Nevertheless, one may continue to find newer compilers that recognize this descriptor as an extension to the definition of Fortran-95/2003. The name for this descriptor is "Hollerith" (named after the inventor of the "IBM" computer card) and the characteristic letter is H. When using this edit descriptor, one must count the total number of characters in the string and indicate this number before the letter H. As an example, to print out the five character literal string HELLO, one would write: 5HHELLO.
I10 An integer field which can hold a maximum of 10 digits. If this is used in an output FORMAT statement and a number less than 10 digits is requested, the number will appear right-justified in the field. E.g. bbbb136787 (wherebindicates a blank space).F10.3 A fixed real number field which can hold a real number containing 6 digits before the decimal point and 3 digits to the right of the decimal point. E.g., bbb123.340
(Note that the decimal point takes one space.)E10.2 A real number field in which the number is written in scientific notation. This allows only 3 digits (maximum) before the decimal point and 2 afterward, since the other spaces are taken up with the decimal point and exponent. E.g., b-0.23E+03
(Note that on most systems, on output the only digit to the left of the decimal point is a zero, and the first digit to the right of the decimal point is non-zero.)3X A blank field which forces 3 blank spaces (between fields). 'HELLO' A literal field which could be used to print out the literal string HELLO.
If the same file is typed on a computer screen or printed on a personal printer, what happens depends on the system used and the default method of printing a file (often based on its extension). Sometimes the "carriage control" characters do not affect the screen or the printer, and these characters are actually seen. Othertimes, the screen or personal printer behaves the same ways as a line printer would behave.
One way of thinking about "carriage control characters" is to imagine the first "output" character being placed in a special (non-printing) column "0" or (Carriage Control) column "cc" which comes right before the first printable column #1.
The standard carriage control characters (and the corresponding actions for Fortran-95 and earlier versions of Fortran) are:
NOTE: the + would be used in special cases, as when one needed to underline certain words.
blank print on the next available line 1 perform a top-of-form before printing (i.e., go to the top of the next page) 0 skip a line before printing (i.e., double-space) + overstrike the last line
Local implementations frequently would augment these basic carriage control characters with other possible characters. In general, though, most other characters merely cause the rest of output line to be printed on the next available line.
Usually, for FORMATs used for output, the standard practice
to insure that no accidents occur (with unintended
numbers/characters being put into the carriage control column)
is to begin the list of edit descriptors with a 1X or
' '. (Older code frequently used 1Hb.)
NOTES:
PROGRAM EXAMPLE_ONE
REAL A, B
INTEGER I
A = 2.0
B = 5.321
I = 126
WRITE(6,121) A,I,B
121 FORMAT(1X,F5.3,I5,F6.2)
STOP
END
OUTPUT (Fortran 95 and earlier):
column numbers --->
cc 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
2 . 0 0 0 1 2 6 5 . 3 2
EXPLANATION:
The first edit descriptor, 1X, forces one blank to be the initial item in the output line. Since this is the first item determined by the FORMAT, it is used as the carriage control character (or, we may just as easily consider it to be placed in "column cc".)
Columns 1-5 were set aside for the value of the variable A by the next descriptor F5.3. Since 3 decimal places were requested, three decimal places were given--all zeroes, but this necessitated the decimal point going in column 2.
The next 5 columns (columns 6-10) were set aside for the value of the integer variable I. That value was printed right justified in that field, i.e., the last digit was printed in the last column (column number 10).
Then the next 6 columns (columns 11-16) were set aside for the value of the real variable B. Only two decimal places were requested, necessitating the decimal point to go in column 14. Thus, the number was rounded to two decimal places.
With Fortran 2003, the numeric output is identical, except that the first number is perceived as appearing in column 2 (rather than column 1).
3.8.2 Longer Example
PROGRAM EXAMPLE_TWO
INTEGER I,J,K
REAL A,B,C
A = 123.45
B = 6.7
C = .89
I = 1
J = 234
K = 56789
WRITE(6,521) A,I,B,J,C,K
521 FORMAT(1X,F10.3,I5///'0',F8.2,2X,I5///1H ,'C=',E10.3,I7)
STOP
END
OUTPUT (Fortran 95 and earlier):
line| num.| | | --> column number v | cc 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -------------------------------------------------------------------- 1 | 1 2 3 . 4 5 0 1 2 | 3 | 4* | 0 5 | 6 . 7 0 2 3 4 6 | 7 | 8 | C = 0 . 8 9 0 E + 0 0 5 6 7 8 9 9 |* Line 4 - The 0 in the carriage control column indicates that the rest of the output line is actually printed on the next available line. The 0 itself never appears. OUTPUT (Fortran 2003 and printers that do not support carriage control characters):
line| num.| | | --> column number v | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -------------------------------------------------------------------- 1 | 1 2 3 . 4 5 0 1 2 | 3 | 4 | 0 6 . 7 0 2 3 4 5 | 6 | 7* | C = 0 . 8 9 0 E + 0 0 5 6 7 8 9 8 |* Line 7 - We assume that the compiler still recognizes the Hollerith edit descriptor H even though it was omitted from the definitions of Fortran 95/2003.
One way is to put all information contained in the FORMAT statement within the input/output statement where the FORMAT number normally goes. The information is included within parentheses as a quoted character string. For example, instead of writing,
WRITE(6,101) A,B
101 FORMAT(1X,F10.2,2X,E10.2)
using Fortran-90/95/2003 style, one could write:
WRITE(6,"(1X,F10.2,2X,E10.2)") A,BThis is similar to the style used in printf statements in C, where format specifiers and variables are both included in one statement but in separate sections. This combined style is actually permitted in FORTRAN-77, although rarely used in practice.
An alternative method is to use a character variable in the input/output statement in place of the FORMAT number. The character variable receives as its value a string of formatting information when declared. To rewrite the previous example, one first declares a character identifier such as FMT1 as follows:
CHARACTER(LEN=*),PARAMETER::FMT1="(1X,F10.2,2X,E10.2)"Then, in the executable section of the program, one writes:
WRITE(6,FMT1) A,B
In this declaration, the * as the value for LEN indicates
that the length of FMT1 matches that of the character
string assigned to it.
100 FORMAT(3I2,2F10.5)is equivalent to
100 FORMAT(I2,I2,I2,F10.5,F10.5)
READ(5,101,END=205) A,B,...reads information from the data file, and when an end of file is reached (i.e., when there are no more data in the input file), statement number 205 is executed and the program continues from there. (This is similar to the action of a "GO TO" statement, cf. section 18.2). This feature would be particularly useful if one does not know the amount of information in the input file, and one wishes to read in all the data before proceeding any further in the code. One would normally make use of such a statement within a loop structure (cf. Chapter 6).
An alternative to the END= option is to use the IOSTAT option in a READ statement. To use this option, one first declares an integer variable that will store the "status value" of the input file after a READ. If the value becomes negative (-1 on most systems), an attempt has been made to read past the end-of-file. One would normally place a READ statement within a loop structure (cf. Chapter 6). An example of this option (with the preliminary variable declaration and assignment) is the following:
INTEGER IO
...
IO = 1
...
READ(20,100,IOSTAT=IO) A,B
If, after this sequence of statements, IO has been changed
to a negative value, the end of file has been reached.
Both the READ and WRITE statements may also include other control information options within the parentheses. See a manual for the version of Fortran being used for further information.
In addition to the "standard" devices, Fortran allows a user to associate arbitrary "device" or "unit" numbers with specific data files stored on disk. Sometimes this can be done at execution time, by just listing the device number and the corresponding file name, but this can also be done from inside the program.
To specify input and output file names and associate them with user-chosen "device" or "unit" numbers, one uses an OPEN statement. To disassociate a given number from a given file, one uses a CLOSE statement.
An OPEN statement consists of the key work OPEN followed by a set of parentheses containing keywords, each followed by an equals sign followed by a specification character string, which varies according to the system being used. More commonly, one need only put in the OPEN statement the most important features, for example, the device number that one wishes to use, the filename which will be associated with that device number, and what type of file it is.
The more common keywords are UNIT (which is given the value of the device number used in READ/WRITE statements), FILE (given the value of file name as a literal character string), ACCESS (which can be given the values 'SEQUENTIAL' or 'DIRECT'), BLANK (with possible values 'NULL' or 'ZERO'), and STATUS (with possible values 'OLD' or 'NEW').
A typical invocation of OPEN is as follows:
OPEN (UNIT=6,FILE='PROG3.INP',ACCESS='SEQUENTIAL',BLANK='ZERO')This would open file "PROG3.INP" as a sequential file, associate it with number 6 (for use in READ and WRITE statements), and assume that all blanks are to be interpreted as zeroes (cf. section 4.3).
To disassociate device number 6 from file "PROG3.INP," one would use the command CLOSE(6).
To inquire about the status of an specific device (previously OPENed), one may use the INQUIRE statement, with appropriate (variable) arguments which would store the desired (but presently unknown) information. For example,
INQUIRE (FILE='PROG3.INP',OPENED=LOGVAR,NUMBER=N)would return a logical value (.TRUE. or .FALSE.) in variable LOGVAR (a logical variable indicating whether the file 'PROG3.INP' had been opened or not) and an integer value in variable N (whose value is the associated "device number"). Both of these values could be used subsequently in the program.
An OPEN statement must be used any time a device number needs to be associated with a file before it is used in a WRITE or READ statement.
The OPEN, CLOSE, and INQUIRE statements may include other control information options within the parentheses. See a manual for the version of Fortran being used for further information.
This page is maintained by Dennis C. Smolarski, S.J.
dsmolarski@math.scu.edu
© Copyright 1998-2005 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 6 June 2005.