Lecture 7 14

Formatted Input

The general form of a formatted read statement is given by

READ format-specifier , input-list

Note the comma (,) in front of the input list. In formatted input, exact spacing of input values is the key requirement, followed by an exact one-to-one match of the TYPE of variable in the input-list, FORMAT statement, and the actual value to be input.

For input, the same format descriptors and rules apply, but we have the following additional rules.

·  Each time a READ is executed, a new line of data is required.

·  The slash (/) may be used in formatted input to read from the next line of data, or to skip a line of input data.

·  When a READ is executed for a character variable, all characters in the field width specified by Aw are read. The input data itself is NOT enclosed in quotes.

·  When a READ is executed for an integer variable, the integer number must be placed (entered) as right-justified in the w columns specified by Iw. If it is not right-justified, zeros are implied in the field width w after the last digit in the integer number. NOTE: in FORTRAN 90, the actual rule says that blanks within numeric fields are ignored. I would advise you to stick with the pre-90 rule and place integers right justified since you may not know which compiler was used to generate an executable code that is provided to you by someone else.

·  When a READ is executed for a real variable and the format Fw.d is being used, the real number may be placed anywhere in the field given by Fw.d if a decimal point is placed in the number. The decimal point in the input value will supercede the number of decimal digits specified by Fw.d (i.e., only the field w is really used in this case). If a decimal point is not present in the input, the decimal point is implicitly placed using the format descriptor; i.e., the decimal point is placed according to the “d” in Fw.d .

·  When a READ is executed for a real variable and the format Ew.d (or ESw.d or ENw.d) is being used, the exponent which is an integer must be right-justified in the field of length w.

·  Real numbers may be input with or without a decimal point. If a decimal point is not entered, it is assumed to be to the right of the last numeric digit present

Formatted input requires precise adherence to the rules noted above, i.e., if the spacing is wrong, the values assigned to a variable will be wrong. Consequently, formatted input is used primarily when input is read from a data file (next subject) and quite often that input is generated by another program (such as Excel or special data generation programs where the input data can be written to a file for input to a Fortran program).


Examples (I format):

Input 15 and 2987 into I and J (integer variables)

READ 5, I, J

5 FORMAT (2I5)

The input provided is:

15 2987

------

Suppose the input was given as

15 2987

------

With a format of 2I5, I will be read as 150 under pre-Fortran 90 rules but as 15 with a program compiled with Fortran 90. Confusing? Yes, that is why I advise you to stick to right-justification of integers unless you are positive the program has been compiled with Fortran 90.

Examples (F format):

Input 15.3 and 29.87 into X and Y (real variables)

READ 5, X, Y

5 FORMAT (2F6.2)

The input provided is:

15.3 29.87

------

or

15.3 29.87

------

or

15.3 29.87

------

Suppose you had the following READ and input data:

READ 5, X, Y

5 FORMAT (2F6.2)

The input provided is:

153 2987

------

The value of X is 1.53 and Y is 29.87. Without a decimal point, the format takes over. The format says two decimal digits, so in both cases, the decimal point is placed to the left of the two right-most digits in the field width of 6.

This is only used in a couple of cases:

1. You are trying to save spaces in a line of input by leaving out the decimal point. This in used in “NASA two-line orbital data that specify the orbital elements for earth satellites.

2. You messed up big time and simply forgot the decimal point!!!


Examples (A format) – for character input:

Input “x-velocity” and “y-velocity” into Velocity_x and Velocity_y

CHARACTER (10) :: Velocity_x, Velocity_y

! or can use CHARACTER :: Velocity_x*10, Velocity_y*10

READ 5, Velocity_x, Velocity_y

5 FORMAT (2A10)

The input provided is:

Velocity_xVelocity_y

------

If you specified the format as

5 FORMAT (2A)

The field width (w part of Aw) would be taken from the length of each character (from the CHARACTER statement)

Suppose you had

READ 5, Velocity_x, Velocity_y

5 FORMAT (2A10)

The input provided is:

Velocity_x Velocity_y

------

Velocity_y will be assigned the character value “ Velocity”

Moral of the story: You have to make the input correspond exactly to what the format specifies.


Skipping input characters and lines of input

You can use X and T to skip over input characters and slash (/) to go to the next line of input or skip an entire line of input.

For example,

READ 5, X, Y,Z

5 FORMAT (F5.2, 5X, F10.0 / E10.2)

The input provided is:

15.3 29.9 3.2 18.69E-5

------

-3.2E5

------

The following values are assigned:

X = 15.3, Y = 3.2, Z = -3.2E5


One way to foul up the input:

Suppose you have

READ 5, X, Y,Z

5 FORMAT (3F5.0)

The input provided is:

15.3 29.9 3.2

------

Assigns: X = 15.3, Y = 29.9, Z = 3.2

Suppose, however, the input provided is:

15.3

------

29.9

------

3.2

------

Assigns: X = 15.3, Y = 0.0, Z = 0.0


The General READ and WRITE statement

WRITE (control-list) output-list

READ (control-list) input-list

control-list includes the following separated by a comma:

·  unit-specifier in the form

·  UNIT=unit-specifier or unit-specifier

·  unit-specifier is a positive integer or integer variable

·  control-list in the form

·  FMT=format-specifier or format-specifier

·  format-specifier is an asterisk (*) or the integer label of a FORMAT statement

The unit-specifier allows the use of many input or output files to be used in the same program at the same time.


Some examples:

Write to unit 7 using free-field format:

WRITE (7,*) A, B

WRITE (UNIT=7,*) A, B

Write to unit 7 using free-field format:

WRITE (7,*) A, B

WRITE (7,FMT=*) A, B

Write to unit 7 using Format statement 35:

WRITE (7,35) A, B

WRITE (7,FMT=35) A, B

The different styles of unit and format specifiers may be mixed (use the format that you like!). Examples for READ statements are exactly the same as above (substitute READ for WRITE)

Personally, I prefer the simplest: WRITE (28,345) A, B which means “Write the variables A & B to output file #28 according to format 345.”


Opening and Closing Files (OPEN and CLOSE statement)

Before you can use a file for input or output, you must “define” it with the OPEN statement. The general form of the OPEN statement is

OPEN (open-list)

The open-list provides a list of information about the file. As a minimum it must include the following.

UNIT = unit-specifier ß (integer constant or variable)

FILE = character-expression ß (name of the file)

Optional information includes the status of the file

STATUS = “OLD” ß file already exits

STATUS = “NEW” ß file should be created

STATUS = “REPLACE” ß file should replace existing one

And whether the file can be read or write only

ACTION = “READ” ß file is for read only

ACTION = “WRITE” ß file is for write only

ACTION = “READWRITE” ß file is for read or write


There are several other file definitions/atributes – see the text.

If the STATUS is omitted, “most” compilers assume NEW.

If the ACTION is omitted, “most” compilers assume READWRITE.

An example:

CHARACTER(11) :: file_name; INTEGER :: file_number

file_number = 17; file_name = “output1.dat”

OPEN (UNIT = 17, FILE = “output1.txt”)

OPEN (UNIT = file_number, FILE = file_name)

OPEN (UNIT = 17, FILE = file_name)

All of the above are identical OPEN statements. Because the STATUS is omitted, it is “assumed” that the file will be created by the program.

CLOSE statement – the CLOSE statement is used to indicate that the file is no longer needed by the program and should be closed.

CLOSE unit-specifier

Example: CLOSE 17 or CLOSE file_number