Unbuffered output in mixed C / Fortran programs

Tobias Burnus burnus@net-b.de
Wed Oct 15 15:01:00 GMT 2008


Hello Georg,

Georg Denk wrote:
> I stumbled on a problem with unbuffered output, and I don't have any clue how to solve this. The
> problem occurs in a mixed C / Fortran program in which I would like to have any output to
> stdout being unbuffered (output to files should be kept buffered for performance reasons). It seems
> that the output sequence depends if stdout is piped or not. If it is not piped, the output appears
> in the sequence it was produced (by printf or WRITE). If, however, the output is piped,
> all output produced by C comes first, followed by the Fortran output.
>   

Try FLUSH. The following uses a Fortran 2003 feature:

      SUBROUTINE FORTRAN_OUTPUT
      USE ISO_FORTRAN_ENV   !!! make OUTPUT_UNIT available
      WRITE(*,*) 'HELLO WORLD, FORTRAN (should be 1st)'
      FLUSH(OUTPUT_UNIT)  !!! FLUSH standard out (stdout)
      RETURN
      END

Besides OUTPUT_UNIT, you can use INPUT_UNIT and ERROR_UNIT or any
numerical unit such as "FLUSH(99)".

In C one can use, e.g.,  fflush(FILE *stream) to do the same there; e.g.
"fflush(stdout);"

Tobias



More information about the Fortran mailing list