<<

Basic I/O Concepts – FORTRAN 90 Chapter 5 of Textbook Formatting output in FORTRAN is incredibly flexible and can be complicated. We will not utilize most of the capabilities. Instead, we will concentrate on the most useful . This handout is designed to give you the basics of formatting input and output in your FORTRAN programs. A thorough reading of Chapter 5 in the text may be needed to solidify your understanding and use of these statements. It will also give you some more advanced understanding of items not covered in this handout.

I/O Descriptors The following descriptors are used to properly format our data.

• I output • F real output • E real output (exponential notation) • ES real output (scientific notation) • L logical output (T or F) • A character output • X space output • T tab output (we won’t cover beyond this) • / slash descriptor – used to insert blank lines

Symbols used with format descriptors

Symbol Meaning Column number d Number of digits to right of the place for real input or output m Minimum number of digits to be displayed n Number of spaces to skip r Repeat count – the number of times to use a descriptor or group of descriptors w Field width – the number of characters to use for the input or output

(This table is from the textbook p.178)

Theses symbols will be referenced in the next two tables.

WRITE Statements (p. 173 – 196) WRITE() statements may be written in several different formats. We are going to introduce only the two most common. You are free to review and use the others as you see necessary. Page Descriptor Data Type Data Value Form Output at 178 rIw integer 317 I3 317 rIw.m I4 ..317 178 rFw.d real (floating 37.9139 F6.3 37.914 point) F7.1 …37.9 F8.5 37.91390 179 rEw.d exponential 180 rESw.d scientific We won’t be using these format descriptors. 181 rLw logical 182 rA character I love CS154! A I love CS154! rAw A12 I love CS154 A13 I love CS154! A17 ….I love CS154!

NOTE: (.) equals a space in above examples! 182 nX space X . -182 3X … 182 Tc tab (to a col. T10 Tabs to col 10 -183 #) T35 Tabs to col 35 184 / newline /// Enters 3 blank lines Now let’s put it all together in a WRITE statement

WRITE(*, *) 'The 2004 Cubs will win', wins, 'games!' WRITE(*, 10) 'The 2004 Cubs will win', wins, 'games!' WRITE(*, 10) 'The 2004 Cubs will win ', wins, ' games!'

10 FORMAT(10x, a, I3, a)

Produce the following:

The 2004 Cubs will win 105 games! The 2004 Cubs will win105games! The 2004 Cubs will win 105 games!

Let’s examine the differences and figure out why they occurred. The first WRITE() statement uses the freeform (*,*) format and is by far the most forgiving and easiest to use. FORTRAN will handle all of the formatting using the minimum amount of space necessary to hold the data to be displayed and separate each data item by a single space. The second and third WRITE() statements use the 10 FORMAT() statement but achieve different results. This is important to notice! The second statement runs our words together making them unreadable because we did not include any spaces inside of the single quoted strings. The third statement prints as expected because we have included a space both after win and before games. The last item I want you to notice about the output is that the output using the FORMAT() statement start 10 columns farther to the right. This is a result of using the 10x descriptor in the format statement. Good programming states that you should always start your output at least one space to the right of the margin. This is not going to happen with the WRITE(*,*).

We don’t want to confuse or overwhelm you with the plethora of options available so we will limit our discussion to what we have covered. Be aware that you will see many variations in the text book and elsewhere. If you can’t figure out what the format statements are doing come to office hours or talk to your lab instructor for further assistance.

READ Statements (p. 197 – 202) READ() statements like WRITE() statements may be written in many different formats. We are going to introduce only the basics. You are free to review and use the others as you see necessary. Page Descriptor Data Type Data Value Form Value Saved in in variable read() 198 rIw integer 317 I3 317 rIw.m I4 317 178 rFw.d real (floating 37.9139 F6.3 37.913 point) F7.1 37.9 F8.5 37.91390 179 rEw.d exponential 180 rESw.d scientific We won’t be using these format descriptors. 181 rLw logical 182 rA character I love CS154! A I love CS154! rAw A3 54! A5 S154! A9 ...CS154!

NOTE: (.) equals a space in above examples! 182 nX space X . -182 3X … 182 Tc tab (to a col. T10 Tabs to col 10 -183 #) T35 Tabs to col 35 184 / newline /// Skips 3 lines

Please look at the examples in chapter 5 for more detail.