Part 9. File I/O

(c) 2017 by Barton Paul Levenson



File basics

I/O, if you didn't know, stands for "Input and Output."

To have records of your results, or to make sure you don't have to enter the same information over and over again, you can use files. The basic process is:

You open the file.
You read it or write to it.
You close the file.



File-handling statements

The Fortran open statement, in its briefest useful form, is


    open(unit=n, file=f)


where n is a "unit number" and f is a string holding a file specification. In old versions of Fortran, unit 5 was the card reader and unit 6 was the line printer. Modern versions tend to reserve the first few units for their own bizarre internal uses, so it's best to use unit numbers of 10 or more. The numbers don't really mean much.

f on a Windows system would be something like


'C:\data\records.dat'


The Fortran close statement is


    close(unit=n)


To read and write to a file, you use the same read and write we used for the keyboard and the screen, but the unit number is the file's unit number, instead of an asterisk.



A file-handling program

Here's a complete example:


! Program to create a file, then read it back.
program fileStuff
    integer       :: i
    character(14) :: record

    open (unit=10, file='squares.dat')

    write (10, 25)                ! This writes 2 lines.
25  format('Number  Square'/ &
    &      '------  ------')

    do i = 1, 12
        write (10, 35) i, i * i   ! This writes 12 more lines.
35      format(i6, i8)
    end do

    close(unit=10)

    open (unit=11, file='squares.dat')

    do i = 1, 14
        read  (11, 45) record
45      format(a)
        write (*, *) record
    end do

    close(unit=11)
end program fileStuff


This program creates a file of the numbers from 1 to 12 and their squares. Then it reads the file back and prints it to the screen. Note that it writes the first two records as strings, then uses a format statement to write numbers, then reads back all the records as strings. You can do this because this is a text file, the default file type.

Fortran also allows binary files, and files you can both read from and write to. But that's for an advanced course. It's more for database work than science or math programming.



Error-handling

Things can go wrong when you do I/O. A file might be missing, data might not match a format, etc. Fortran has some, rather primitive, ability to handle errors. In any file statement--open, close, read, write, etc. (and there are others), you can include any of three important clauses. The first is:


err=L


where L is a line number, like the numbers on format statements. This will abort an I/O operation in case of an error, and transfer control to the line you specify.

The second optional clause is:


end=L


This also branches to a line you specify, in case an end-of-file condition is encountered. With this method, we could have coded the second loop in the file-handling program like this:


    do
        read(11, 45, end=55) record
45      format(a)
        write (*, *) record
    end do

55  close(unit=11)


The last optional clause of interest is:


iostat=iVar


where iVar is the name of an integer variable. In case of an I/O error, this puts the Fortran error number in the variable you supply (here named iVar, although, of course, you can call it anything you want). You can then look up what happened in a list of Fortran errors. You can find these in the Plato help files. Search for "errors" and choose "Execution errors and IOSTAT values." Unfortunately, there are hundreds of the damn things.





Page created:05/05/2017
Last modified:  05/05/2017
Author:BPL