0

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?

2 Answers2

0

A FORTRAN code will, unless given explicit direction otherwise, write to output "when it feels like it", or when the system feels like allowing it. It's all about buffering. The call to flush is put in here precisely for that reason: The writer wanted to force an output right there, for obvious reasons. However, I have never seen a code that needed a flush in this context. I would experiment with replacing write(*,*) with print *,.

ANother possibility might be to compile your code so that output buffering is not allowed. Check the man pages for the appropriate flags, if they exist.

bob.sacamento
  • 6,283
  • 10
  • 56
  • 115
  • Thanks Bob. I'm the one who added the 'call flush(6)', and, like you, didn't feel it necessary until I noticed the output was buffered. I tried print without luck. It's curious I tell you! Curious! – Koenig Cochran Jun 10 '13 at 11:14
  • OK. Sorry I couldn't be of more help. Best of luck. Let us know the answer if you ever find it! – bob.sacamento Jun 10 '13 at 19:34
0

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.