# Read and write text files in Modern Fortran

We will read and write text and numeric data into a file using modern fortran.

In this post, I will try to give you a quick look of the key basic concepts in modern Fortran so that we can write a simple text file using the Fortran completely. Then we will read the file using Fortran.

## Basic fortran program

Let us write a program to print the area of a circle in your stdout (your terminal).

``````program maths
implicit none
real :: r, area
real, parameter :: pi = 4.*atan(1.)
r = 2.0
area = pi*r**2

print*,'The area of a circle of radius ',r,' is ',area
end program maths
``````

Notice that I have defined a parameter using the trigonometric function `atan`. The good thing about Fortran is that the mathematical functions are embedded into it.

Let us compile this code and run it:

``````gfortran -o circarea circarea.f90
./circarea
``````

This returns:

`````` The area of a circle of radius    2.00000000      is    12.5663710
``````

The results are correct but as you may have noticed that the output is not formatted properly. So, let us fix that.

``````program maths
implicit none
real :: r, area
real, parameter :: pi = 4.*atan(1.)
r = 2.0
area = pi*r**2

write(*,1) 'The area of a circle of radius ',r,' is ',area
1 format(A,F3.1,A,F5.2)
end program maths
``````

This time we have used the `write` to output the result instead of `print`. You can also notice that we used the format to properly format the output:

``````The area of a circle of radius 2.0 is 12.57
``````

## Fortran format descriptors

Descriptor Description Example
I integer output `print "(3i5)", i, j, k` repeat `i5` three times
F real number output `print "(2f12.3)",val1, val2` repeat 12 field width twice with precision 3
E real output in exponential notation `print "(e10.3)",123456.0` gives ‘0.123e+06’
A character output `print "(2a)", 'earthinversion'` gives ‘earthinversionearthinversion’
X space output `print "(a,2X,a)", 'earthinversion', 'earthinversion'` gives ‘earthinversion earthinversion’
/ insert blank lines `print "(a,/,a)", str, str`

## Writing to a file

Writing and later you will see that the “reading” is about using the `open`, `close`, `write` and `read` statements.

``````program maths
implicit none
real :: r, area
real, parameter :: pi = 4.*atan(1.)
r = 2.0
area = pi*r**2

! write to ascii file
open(unit=1,file='results.out',status='replace',form='formatted')
write(1,11) 'The area of a circle of radius ',r,' is ',area
11 format(A,F3.1,A,F5.2)
close(1)
end program maths
``````

``````program maths
implicit none
real :: r, area
real, parameter :: pi = 4.*atan(1.)
character(len=20) :: filename
character(len = 45) :: str1
r = 2.0
area = pi*r**2
filename='results.out'

! write to ascii file
open(unit=1,file=filename,status='replace',form='formatted') !rewrite the file if exists
write(1,11) 'The area of a circle of radius ',r,' is ',area
11 format(A,F3.1,A,F5.2)
close(1)

open(unit=2,file=filename,status='old') !read from the existing file
close(2)

write(*,*) str1
end program maths
``````

This program writes a string into a file ‘results.out’ and then read it back into a string named `str1`. Notice that we have defined the length of the string long enough (45 chars) to read all the string data.

## Read and write the numeric data into a file

This time instead of writing it as a string, we will only write the numeric data into the file and read those data back into another variables.

``````program maths
implicit none
real :: r, area
real :: r_rd, area_rd
real, parameter :: pi = 4.*atan(1.)
character(len=20) :: filename
r = 2.0
area = pi*r**2
filename='results.out'

! write to ascii file
open(unit=1,file=filename,status='replace',form='formatted') !rewrite the file if exists
write(1,11) r,area
11 format(F3.1,F5.2)
close(1)

open(unit=2,file=filename,status='old') !read from the existing file
close(2)

write(*,12) r_rd, area_rd
12 format(F3.1,2X,F5.2)
end program maths
``````

Tags:

Categories:

Created on:

#### Disclaimer of liability

The information provided by the Earth Inversion is made available for educational purposes only.

Whilst we endeavor to keep the information up-to-date and correct. Earth Inversion makes no representations or warranties of any kind, express or implied about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services or related graphics content on the website for any purpose.

UNDER NO CIRCUMSTANCE SHALL WE HAVE ANY LIABILITY TO YOU FOR ANY LOSS OR DAMAGE OF ANY KIND INCURRED AS A RESULT OF THE USE OF THE SITE OR RELIANCE ON ANY INFORMATION PROVIDED ON THE SITE. ANY RELIANCE YOU PLACED ON SUCH MATERIAL IS THEREFORE STRICTLY AT YOUR OWN RISK.