Fortran Wiki
Extended Re-OPEN capabilities

Rationale: It is often useful to change I/O attributes without disconnecting a unit that may be connected to an unnamed pipe (common with standard I/O units). This is currently supported when issuing an OPEN command with matching UNIT and FILE values, but only for a limited set of properties. For example, it can be useful to set standard-input to a non-default text ENCODING, or set ACCESS=‘STREAM’. It is reasonable to extend the set of modifiable I/O properties only for connected units that have not yet transferred any data.

Often, standard I/O units are unnamed. For those units, re-OPEN should accept FILE=’’, or possibly no FILE specifier.


Comments

Jason Blevins, 20 July 2010: I think being able to re-open pre-connected units such as stdio to use stream I/O is very important. This would make writing Unix-style text filters much more natural. There was some discussion about this on comp.lang.fortran last year in the following threads:


Compiler Behaviors

The descriptions below are for the standard input unit. Similar behaviors occur for the standard output and error units.

For many Fortran compilers, and INQUIRE on INPUT_UNIT returns the unit as unnamed. Many return the dummy filename “stdin”, which is generally a bad convention because “stdin” is also a valid filename. In both cases, there is no proper way to issue a re-open on a standard I/O unit, unless the default filename for INPUT_UNIT does not follow the normal convention of “fort.5”.

Intel Fortran

Intel Fortran seems to handle standard I/O units the best. It returns actual tty names where possible. However, when tty names are not available, it returns “stdin”, which is really incorrect.

The default unit is typically "fort.number" where number is the logical UNIT number. Intel Fortran defines the default file for UNIT=INPUT_UNIT to always be stdin, rather than a named file. This is the best way to support re-opening standard I/O units without any Standards modifications. This does not completely solve the problem for extended re-open attributes, because some forms of I/O may be interrupted by explicitly closing the unit first.

When an OPEN is issued with INPUT_UNIT and no FILE specifier, Intel Fortran will attempt to open or re-open stdin. This can be used to re-open stdin with modified attributes, including ACCESS=‘STREAM’. However, there is currently that causes STREAM I/O to fail for all pipe/tty devices (reported; hopefully fixed soon.)

GFortran

GFortran always returns “stdin” for standard input. (This may change, at least for tty devices.) You can re-open the connected stdin by not giving a FILE= specifier. It does not accept FILE=’’. Once stdin has been closed, this behaves as FILE=‘fort.5’, creating an empty fort.5 file if it does not yet exist.

G77

INQUIRE returns stdin as unnamed. Reopening works as in GFortran, with UNIT=INPUT_UNIT and FILE= unspecified.

Sunf90

Behaves generally the same as G77.