next up previous contents
Next: REWIND and BACKSPACE statements Up: General I/O Previous: UNFORMATTED or BINARY I/O

   
OPEN and CLOSE statements

The general forms of the OPEN and CLOSE atatements are:-

OPEN(parameter,parameter,...) CLOSE(parameter,parameter,...)
where parameter has one of the following forms :-

keyword keyword=value

All parameters, except UNIT are optional. This parameter has the form:-

UNIT=expression
This defines the logical unit number and it is good programming practise to define an integer variable for every unit number - see for example the program shown in section 10.1. The other parameters most useful to someone new to FORTRAN are shown below:-

FILE
=character expression for file name

STATUS
='OLD'-file already exists ='NEW'-file is to be created ='SCRATCH'-temporary file to exist only for job duration ='UNKNOWN'-file may or may not exist

ACCESS
='SEQUENTIAL') = 'DIRECT'-see section 10.5

ERR
=statement label - label to goto should OPEN/CLOSE fail

FORM
='FORMATTED'

='UNFORMATTED

READONLY
- ensures file is protected against user writing. VMS only.

The above parameters are normally only supplied with the OPEN statement and all, with the possible exception of FILE, take sensible defaults. The CLOSE statement usually only has the UNIT parameter. The OPEN and CLOSE statements are optional. If a READ or WRITE statement is encountered for a logical unit number that has not previously been declared in an OPEN statement then a default OPEN is given. The default taken is computer dependant and may not be what the user wants. So get into the habit of giving an OPEN for each unit. When the program ends all units that are open have a default CLOSE performed and this may be good enough. However an output file may not be complete, or even exist, until a CLOSE is given so for long running programs that write out intermediate results (sometimes called checkpoints), the file should be closed as soon as possible. If two OPENs are given for the same unit number without an intervening CLOSE, the second will automatically CLOSE the first.

Exercise 13
Write a program that creates a file called number.dat that contains all the numbers from 1 to 10000 in the format 10I5 i.e. the file starts:-
 
         1    2    3  ...
 
        11   12   13  ...
When the program runs type out the first part of it using the command:-
cat number.dat
After typing out the first few lines type Ctrl C to stop it. Use the ls command to see how big the file is :-

ls -l number.dat
Now write a program that reads the file back in and types out the first 10 records. The program should continue to read to the end of the file and then type out the last record and the number of records in the file. Now repeat both prorams but using UNFORMATTED I/O. See how big number.dat is this time but DO NOT try to type it out.
* * *


next up previous contents
Next: REWIND and BACKSPACE statements Up: General I/O Previous: UNFORMATTED or BINARY I/O
n west (APC)
2000-03-08