Search code examples
eclipsefortran90flushgfortranphotran

Why does Eclipse's Photran require 'call flush(6)' after 'write' to prevent buffering to std out?


I'm using Classic Eclipse 4.2.2 with the Photran plugin and cygwin's gfortran compiler on a Windows 7 Professional machine. If I remove 'call flush(6)' below, the program does not write to console until AFTER reading from std in:

program mult1
implicit none

integer :: i,j,k

!
!
! This program just multiplies two integers
! together.
!
!

write(*,*) 'Enter i,j: '
call flush(6)
read(*,*)i,j

k = i*j

write(*,*) 'The product is ', k
stop
end program mult1

Any thoughts or fixes?


Solution

  • I resolved the issue by preventing ALL buffering. This is not the perfect solution (it would be nice to buffer some outputs and not others), but it solves my problem---namely, not having to call flush(6) every time I write(,).

    To prevent all buffering with gfortran (NOTE: It is compiler specific),

    1. Include the environment variable: GFORTRAN_UNBUFFERED_ALL
    2. And assign it the value: 1

    In the event that you are USING PHOTRAN (the eclipse plug-in for Fortran), you will have to do the following:

    1. Navigate in overhead bar to Run>Run Configurations
    2. In the left pane, choose your project in the drop down menu Fortran Local Application> Your Project
    3. In the right pane, select the "Environment Tab"
    4. Click "New" and add the variable and value listed above.