# File IO

Reading from and writing to the console works for simple programs, and is often used even in more complex codes to print error messages, progress indicators, and the like, but for most purposes we need to read from and write to files.

## Open

A file must be opened before it can be accessed by the executable. In general, we associate some type of file descriptor with the name of the file, then after making that connection, henceforth the file is referenced by its descriptor.

In Fortran, files are identified with integers called unit numbers. They are not generated automatically, but must be chosed by the programmer.

OPEN(UNIT=iunit,FILE=fname)


The open command has many other options. Only UNIT and FILE are required. If the unit argument is first it does not need the “UNIT=” keyword.

On Unix file names will be case sensitive . In Unix unit 5 is conventionally standard input and unit 6 is standard output. Standard error is not as uniform but it is usually unit 2.

Programmers can reassign units 2, 5, and 6, but it is strongly advised that you not do so.

## Close

Much of the time, it is not necessary to close a file explicitly. Files are automatically closed when execution terminates.

If many files are opened, it is good practice to close them before the end of the run.

CLOSE(UNIT=iunit)


If you wish to reopen a file for some reason, you must first CLOSE it.

The file must first be opened and a unit assigned.

READ(iunit,*)


List-directed output is indicated by an asterisk. Formatted output requires a format string, or a reference to a labeled FORMAT statement.

WRITE(iunit,*)
WRITE(iunit,'(fmtstr)')


or

      WRITE(iunit,label)
label FORMAT(fmtstr)


If the unit identifier is not the first in the list it must be written as UNIT=iunit. The UNIT= keyword is optional otherwise.

## NAMELIST

One of the most convenient I/O statements in Fortran is NAMELIST. With this statement, parameters in an input file can be specified by name=value pairs and in any order.

The namelist must be declared. This is a non-executable statement. The syntax is:

NAMELIST /name/ var1,var2,var3


The name is chosen by the programmer. The namelist is read with a special form of the READ statement.

read(iunit, name)


## Namelist Input

The input file containing the namelist must follow a specific format. Namelist was not part of the Fortran 77 standard so there is some variation. However, thenamelistalways starts with

&name


The variable list follows, with each variable on a separate line and consisting of the varname=value pair. In older code, the namelist frequently terminates with another ampersand &, or &end. Also, in Fortran 77 there may be rules about in which column the & can occur.

Namelist was established as part of the standard in Fortran 90. According to the standard, the namelist is terminated with a forward slash /.

Example In the program

NAMELIST /params/ rho,eps, x0

OPEN(10,file='paramlist.txt')


The input file (Fortran 90 format) would be

&params
rho=1.3
eps=1.e-7
x0=0.0
/


Exercises

1. Write a program that creates a file mydata.txt containing four rows consisting of
1, 2, 3
4, 5, 6
7, 8, 9
10, 11, 12


Close the file, then open it again. Read the data back. Write a loop to add 1 to each value and print each row to the console.

Example Solution

program reopen
implicit none

character(len=50) :: filename
integer           :: i,item1,item2,item3

filename="mydata.txt"

open(unit=10,file=filename)

do i=1,12,3
write(10,*)
enddo

close(10)

open(unit=11,file=filename)
do i=1,4
item1=item1+1
item2=item2+1
item3=item3+1
print *, item1,item2,item3
enddo

end program



1. Write a program that reads the params namelist and prints the variables to the console. Create the paramlist.txt file and test your program.
Example Solution

program read_params
implicit none

real   ::  rho, eps, x0
namelist /params/ rho,eps, x0

open(10,file='paramlist.txt')