|
|
|
||||||
|
Flushing Fortran OutputSome of you have complained that the some Unix machines do not write to an output file as often as other machines you have used do. This is because the some machines (ie. HPs) buffer output in larger chunks. However, if doing direct file output from the program, there is a way around this. In fortran, you can close and open the file to force a buffer flush, in C this is accomplished by the fflush command. The following examples do the same thing, c is on the left, fortran on the right. If you use i/o redirection, the fflush() will work for C, if you use fortran see the example of calling fflush from fortran at the end of this message. All three examples, will do the first two print statements, then the last two when the program finishes. Without all this flushing, all four prints happen at the end of the program.
#include <stdio.h>
main () program main
{
int i; implicit none
FILE *fp; integer i
fp = fopen("a","w"); open(unit=9,file='a',status='unknown')
fprintf(fp,"zero\n"); write (9,*) 'zero'
for (i=1;i<=100000000;i++); do i=1,100000000
enddo
fprintf(fp,"one\n"); write (9,*) 'one'
fflush(fp); close(unit=9)
open(unit=9,file='a',status='old',
access='append')
for (i=1;i<=100000000;i++); do i=1,100000000
enddo
fprintf(fp,"two\n"); write (9,*) 'two'
for (i=1;i<=100000000;i++); do i=1,100000000
enddo
fprintf(fp,"three\n"); write (9,*) 'three'
fclose(fp); close(unit=9)
stop
} end
Since you can't really tell fortran to open and close unit 6, (that I know of) you have to create a c program to flush the standard out when doing i/o redirection, follow the example below: input the programs:
f77flush.c:
-----------
#include <stdio.h>
int f77flush()
{
fflush(stdout);
fflush(stderr);
}
a.f:
----
program main
implicit none
integer i
write (6,*) 'zero'
do i=1,100000000
enddo
write (6,*) 'one'
call f77flush()
do i=1,100000000
enddo
write (6,*) 'two'
do i=1,100000000
enddo
write (6,*) 'three'
stop
end
Next do the following: This should have the same output as the other examples, and may indeed be more elegant than the first fortran examples, plus it flushes the errors as well. |
|
If you have trouble accessing this page, or need an alternate format contact webmaster@stat.osu.edu. |