Skip to main content Skip to navigation

3. Input and output

Printing out data

You will already have seen how we can get f90 to output free format information, using print. The syntax for the print statement is as follows

print*, your_data

You will already have seen in the last example, that to print some text plus the value of an integer variable, you had to include a line such as

print*,special

Putting it into Practice:

Suppose you wished to print out four real variables on one line, with each number having 4 decimal places. If you tried to print this out using the free format print statement (eg. see code fragment below) what happens?
a1 = 5.67893454
a2 = 4.65124454
a3 = 10.2342454
a4 = 539.40405324

print*,a1,a2,a3,a4
You should find that the output looks pretty ordered, but each entry occupies 8 spaces. You can fix this using a format statement - try the code fragment below
a1 = 5.67893454
a2 = 4.65124454
a3 = 10.2342454
a4 = 539.40405324

3 format(f10.5,f10.5,f10.5,f10.5)

write(*,3) a1,a2,a3,a4

Note that when formatting output, the write statement is used instead of print. You should also note that print/write will send output to the standard output (your x-term, or a file if you have redirected your output). If you want to write out to a specific file, you have to make a few changes (more later) to your code.

The format statement in the exercise above contains entries called edit descriptors. The edit descriptors in this example will print the number out in gap of 10 spaces, with 4 decimal places. The edit descriptors in this format statement above contain repetition. In this case, you can write the format statement more compactly as

3 format(4f10.5)

Consult any book on fortran90 to learn more about edit descriptors.

Reading in data

There is a counterpart to print/write known as read that can be used for reading in data. In these examples, we will consider reading in data from the standard input (ie keyboard or a data file if you are using re-direction). This command can work in formatted or free format form. The general syntax of free format read is read is:

 read*,your_data

Putting it into Practice:

In this example, you will write code that will read in some text from the keyboard and print it out again. Into a new .f90 file, type in the example code below;
program getname
implicit none
character(len=100) :: name

print*,"please enter your name at the prompt"
read*,name
print*,"your name is ",name

end program getname
Compile and run this code. What happens if you type in a name with a space in it? What happens if you type this name within quotation marks?

Just like with write, the read statement can be modified so that it will read in formatted data. The edit descriptors for the read statement are very similar. Consult a book on f90 to find out more.

Instructions on how to read in input/write out output from/to a specific file, rather than the keyboard are a little different. First you must open the file with an open statement. The open statement specifies the unit number of the file (how fortran refers to the file), the file name, and the file status (new/old/unknown etc). then when you come to print out your output, give the unit number. Finally close the file with a close statement. Try the example below;

/

Putting it into Practice:

In this example, you will write code that will read a name and print the output to a file called myname. Change your existing .f90 file so that it is like the following;
program getname
implicit none
character(len=100) :: name


print*,"please enter your name at the prompt"
read*,name
open(unit=6,file='myname',status='new')

write(6,*) "your name is ",name

close(6)

end program getname
 
Compile this code. Run the code to see if it works - what has been printed to the file myname?. What happens if you run the code immediately again?