FORTRAN 77 Language Reference

Examples

Here are six examples.

Example 1: Open a file and connect it to unit 8--either of the following forms of the OPEN statement opens the file, projectA/data.test, and connects it to FORTRAN unit 8:


       OPEN(UNIT=8, FILE='projectA/data.test') 
       OPEN(8, FILE='projectA/data.test') 

In the above example, these properties are established by default: sequential access, formatted file, and (unwisely) no allowance for error during file open.

Example 2: Explicitly specify properties:


       OPEN(UNIT=8, FILE='projectA/data.test', &        ACCESS='SEQUENTIAL', FORM='FORMATTED')

Example 3: Either of these opens file, fort.8, and connects it to unit 8:


       OPEN(UNIT=8) 
       OPEN(8)

In the above example, you get sequential access, formatted file, and no allowance for error during file open. If the file, fort.8 does not exist before execution, it is created. The file remains after termination.

Example 4: Allowing for open errors:


       OPEN(UNIT=8, FILE='projectA/data.test', ERR=99)

The above statement branches to 99 if an error occurs during OPEN.

Example 5: Allowing for variable-length records;


       OPEN(1, ACCESS='DIRECT', recl=1)

See "Direct Access I/O" for more information about variable-length records.

Example 6: Scratch file:


       OPEN(1, STATUS='SCRATCH')

This statement opens a temporary file with a name, such as tmp.FAAAa003zU. The file is usually in the current working directory, or in TMPDIR if that environment variable is set.