Now take a look at ``read.f'' (or view it directly). This is a very straightforward program that reads in four integers in four different ways, and then prints them all back out. Run the program. The ideas here are very similar to those from the last section.
READ *, X(1)
This is the simple input statement with which you are familiar. Working from analogy with ``PRINT'', what do you suppose the ``*'' means?
READ (UNIT=*, FMT=*) X(2)
Just as with the ``WRITE'' statement, we are now specifying two pieces of information: where the input is coming from, and how the input will be formatted. And as with the ``WRITE'' statement, using the ``*'' leaves the choice up to Fortran. As a result, it will expect the input to come from the keyboard, and it will assume that the input won't be formatted in any particular fashion.
This ``READ'' statement is functionally identical to the one discussed in part (1).
READ (UNIT=5, FMT=*) X(3)
Now we're starting to get somewhere. We're still telling Fortran to use the default formatting conventions, but here we're telling it to read from unit 5. This statement will still read from the keyboard, since unit 5 refers to the keyboard. But once you learn how to tell Fortran about other unit numbers, you'll be able to read from files.
READ (UNIT=5, FMT=75) X(4)
Now we're telling Fortran to read from unit 5 (the keyboard) using format 75. What is format 75?
Try running the program and entering two-digit numbers instead of one-digit numbers. What happens?
As with WRITE, we are only trying to give you the general idea of how READ works. We will look at specifics in upcoming lessons.
Again, you might be wondering which versions to use. If you want to read from the keyboard using the default format, you should use
READ *, ....
If you care about the form, or if you want to read from somewhere other than the display, you should use (for example)
READ (UNIT=7, FMT=300)
This would read from unit 7, using format 300. Again, this assumes that you have told Fortran what unit 7 is and what format 300 means.
Hamlet Project