You are on page 1of 3

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 basics. 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 F E ES L A X T / integer output real output real output (exponential notation) real output (scientific notation) logical output (T or F) character output space output tab output (we wont cover beyond this) slash descriptor used to insert blank lines

Symbols used with format descriptors


Symbol c d m n r w Meaning
Column number Number of digits to right of the decimal place for real input or output Minimum number of digits to be displayed Number of spaces to skip Repeat count the number of times to use a descriptor or group of descriptors 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 integer 317 I3 317 rIw I4 ..317 rIw.m 37.9139 F6.3 37.914 178 real (floating rFw.d point) F7.1 37.9 F8.5 37.91390 179 exponential rEw.d We wont be using these format descriptors. 180 scientific rESw.d 181 logical rLw 182 character I love CS154! A I love CS154! rA A12 I love CS154 rAw A13 I love CS154! A17 .I love CS154! NOTE: (.) equals a space in above examples! 182 space X . nX -182 3X 182 tab (to a col. T10 Tabs to col 10 Tc -183 #) T35 Tabs to col 35 184 / newline /// Enters 3 blank lines Now lets 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! Lets 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 dont 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 cant 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. Value Saved in Page Descriptor Data Type Data Value Form in variable read() 198 integer 317 I3 317 rIw I4 317 rIw.m 178 real (floating 37.9139 F6.3 37.913 rFw.d point) F7.1 37.9 F8.5 37.91390 179 exponential rEw.d We wont be using these format descriptors. 180 scientific rESw.d 181 logical rLw 182 character I love CS154! A I love CS154! rA A3 54! rAw A5 S154! A9 ...CS154! 182 -182 182 -183 184 nX Tc / NOTE: (.) equals a space in above examples! space X . 3X tab (to a col. T10 Tabs to col 10 #) T35 Tabs to col 35 newline /// Skips 3 lines

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

You might also like