Introduction To Programming With Fortran 90: Advanced I/O and Files
Introduction To Programming With Fortran 90: Advanced I/O and Files
Summary
This will describe some advanced I/O features Some are useful but only in Fortran 2003 Some are esoteric or tricky to use
Non-Advancing Output
You can build up a record in sections
WRITE (*, (a), ADVANCE=no) value = IF (value < 0.0) THEN WRITE (*, ("None")) value ELSE WRITE (*, (F5.2)) value ENDIF
That will usually work, but may not The text may not be written out immediately Even using FLUSH may not force that Too many prompts may exceed the record length
Non-Advancing Input
You can decode a record in sections Just like for output, if you know the format Reading unknown length records is possible Here are two recipes that are safe and reliable Unfortunately, Fortran 90 and Fortran 2003 differ
The EOR branch is taken if the record is short The following happens whether or not it is SIZE returns the number of characters read
If IOSTAT is IOSTAT_EOR, the record is short If IOSTAT is IOSTAT_EOF, we are at end-of-le SIZE returns the number of characters read The Fortran 90 recipe works, but this is cleaner
Read the specication, to avoid gotchas Work out exactly what you want to do with it
Use a precision of zero (e.g. F8.0) Always include a decimal point in the number Dont use the P or BZ descriptors for input Dont set BLANK=zero in OPEN or READ
Existence may not mean what you expect E.g. a new, output le may be open but not exist
Unformatted I/O
Using pipes or sockets is tricky So is unformatted I/O of derived types
Namelist
Namelist is a historical oddity, new in Fortran 90 This sounds impossible, but I assure you is true
STREAM Files
Fortran 2003 has introduced STREAM les These are for interchange with C-like les They provide all portable features of C
Asynchronous I/O
Mainframes proved that it is the right approach Fortran 2003 introduced it
For complicated reasons, you should avoid it This has nothing to do with Fortran
Dont use POSIX asynchronous I/O, either And probably not Microsofts . . .
Oddities of Connection
Try to avoid these, as they are confusing
You will see them in some of the references Files can be connected but not exist Ones newly created by OPEN may be like that Units can be connected when the program starts Ask me if you want to know why and how OPEN can be used on an existing connection It modies the connection properties
Other Topics
There are a lot more optional features You must read Fortrans specications for them Fortran 2003 adds many slightly useful features Most compilers dont support many of them yet The above has described the most useful ones And a few features should be avoided entirely For more on this, look at the OldFortran course
Last Reminder
Be careful when using Fortran I/O features They dont always do what you expect It is much cleaner than C/POSIX, but . . . Fortrans model is very unlike C/POSIXs Fortrans terminology can be very odd The underlying C/POSIX can show through In addition to Fortrans own oddities