If you have the "techlib" info CD, you can find this information by
searching on "flush" and "fortran". This is the information it gives:
----------------------------------------------------------------------------
FORTRAN writes to disk are buffered and FORTRAN contains no internal
mechanism, other than closing and re-opening the file, for flushing
the write buffers and forcing the write to disk to take place. The
following provides a work-around to this by using a C subroutine
to perform the flush.
SOLUTION:
To cause an immediate buffer flush of FORTRAN write statements, one
can use the fflush system call from a C subroutine. To use this
function however, FORTRAN must be writing to either standard out (unit
6) or standard error (unit 0). An additional C function can be used
to redirect output from either stdout or stderr to allow the FORTRAN
writes to these devices to actually go to disk. This is accomplished
using the freopen system call. This system call closes a specified
stream, reopens that stream and associates it with the given path.
The following C and FORTRAN code examples illustrate how this can be
accomplished.
FORTRAN
PROGRAM FLUSH
CALL STREAMSWAP()
WRITE(*, *) "This is output"
CALL CFLUSH()
READ (*, *) A
STOP
END
The READ statement is included to suspend execution of the FORTRAN
program so that the flush can be verified by examining the output
file while the program is waiting for input.
C code
#include <stdio.h>
void streamswap()
{
FILE *fp;
fp=freopen("output.out", "a+", stdout);
}
void cflush()
{
int n;
n=fflush(stdout);
}
This code closes standard out and re-opens the stream associated with
stdout and assigns it to the file output.out. The cflush function
should be called from the FORTRAN code whenever a flush is desired.
The flush simply forces output from the stream.
Upon exit from the main program, the stream is disassociated
with the output file and re-associated with standard out.
REFERENCES:
Calls and Subroutines Reference: Base Operation System
fflush command
freopen command
--
-----------------------------------------------------------------------
Lucien Van Elsen IBM Research
luc...@watson.ibm.com Project Agora
> Does anyone have an AIX Fortran code example of calling fflush?
> I have a user who needs it and hasn't gotten it to work.
In the best tradition of not answering your question, you're much
better off writing a fortran callable c routine like:
/*
* Flush output
*/
#include <stdio.h>
void flushout() /* Call it flushout_ on most OSs */
{
fflush(NULL);
}
Compile it yourself on all your machines, stick it in a local library
libocal.a and tell your users to call flushout() from fortran and put
-local on the link line. Works fine here on AIX, HPUX, SunOS and
ConvexOS (calling it flushout_ in the c source for the other OSs).
Doing it directly would be much more messy. How do you pass it a NULL
or the pointer to a filehandle? Golden rule, put all the hardware and
OS dependent stuff in one or two utility routines, never in the body
of your code. In this day and age you'll soon need to run it on a
different machine. Besides it's a lot less confusing for the user.
John Rowe
Dept. Physics
Exeter University
UK