Unbuffered output in mixed C / Fortran programs
Steve Kargl
sgk@troutmask.apl.washington.edu
Wed Oct 15 15:55:00 GMT 2008
On Wed, Oct 15, 2008 at 05:20:46PM +0200, Georg Denk wrote:
> Hello Tobias!
>
> Tobias Burnus wrote:
> > 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);"
>
> Thanks for this hint. To be honest, I was aware of flush, and it might be
> a solution for this simple test case. But for the real thing, there are so
> many output statements that it is more or less impossible to add
> some flush to all of them. That's the reason for the setvbuf call in
> the C part which is similar to adding a "flush" after each printf.
>
Since you're already doing mix language program, you can
use the ISO C binding mechanism to call putenv at the
start of main to set GFORTRAN_UNBUFFERED_PRECONNECTED.
It would look something like
program main
use iso_c_binding
! other declarations
! ....
! first executed statement
call putenv("GFORTRAN_UNBUFFERED_PRECONNECTED=y")
! rest of program
end program main
PS: I wrote the above off the top of head, you'll need to check
the actual syntax.
--
Steve
More information about the Fortran
mailing list