Formatted Input/Output

 

References: Section 10 Fortran95 Manual , the Fortran Lecture2 and the Fortran Lecture5

 

 Input:

                        read (unit, format, options)  variable list

                                    unit = *: read from keyboard

                                    format = *: no specified format or free format

            or         read format, variable list

                                   

Output:

                        write (unit, format, options) list

                                    unit = * : write to the monitor

                                    format = *: no specified format or free format

            or         print format, list

                                   

Letter code for Formatted input/Output :

                        a: character data

                        I:  integer data   (I8: 8 spaces)

                        F: real data in decimal notation

                                    (F12.4  : total number of spaces is 12, 4 digits after the decimal point)

                        E: real data in scientific notation  (E12.4)

                        x: space ( 5x: 5 empty spaces)

 

Exercises

1. What are the three types of unit for READ and WRITE?

2. What are the three types of format for READ and WRITE?

3. Give an example of using options in a READ statement.

4. Give an example of using options in a WRITE statement.

5. What is a record in input/output terminology?

6. In a format specification, what is used for an 8 digit positive integer number; and what is used for an 8 digit negative integer number? Which letter is used for characters? What is used for a real number with 12-space field width and 4 digit of precision? What is used for 10 empty spaces?

7. Using the statements below to print a real number, how many digits are there after the decimal point? The field width is 5. Does it include the decimal point and the sign of the number?

                        WRITE ( *, 150 ) X

      150   FORMAT ( 1X, F5.2 )

 

8. What are the outputs on the PC screen from the following output statements? Use # to indicate empty spaces which are not in text strings.

a)

                        write(*, “( a )”) ‘Enter a real number.’

b)

            write(*,1000) ‘Enter a real number.’

      1000  format( a )

c)

            Print “( a, i2, 4x, a, f6.2, 4x, a, e10.2 )”, ‘integer:’, &

                  77, ‘float:’, 77.0, ‘exponent:’, 77.0

d)

            x = 1.1

            y = 2.0

            z = x*y

            Write(*,1001) x,y,z

      1001  format( 3f12.6)

e)

            x = 1.1

            y = 2.0

            z = x*y

            write(*,1002) x,y,z

      1002  format(1x, f12.6, ‘  multiplied by’, f12.6, ‘  equals’, f12.6)

f)

            x = 1.1

            y = 2.0

            z = x*y

            write(*,1002) x, ‘ multiplied by’, y, ‘ equals’, z

      1002  format(1x, f12.6, a, f12.6, a, f12.6)

 

9. (optional) Modify the program (C2F.f95) so it displays the temperature table with 2 digits of precision.

 

10. (optional)   Use formatted output to draw a tree, as shown below.