OSU Navigation Bar

The Ohio State University

Department of Statistics

Cockins Hall
rollover image OSU Statistics
            Home

design element

OSU Statistics

Home

News

Research & Consulting Groups

People

For Visitors

For Prospective Students

For Current Students, Staff & Faculty

Contact Us



rollover image

For Current Students & Faculty

rollover image

Courses

rollover image

Links

rollover image

Computer Support

rollover image

Internal Documents

rollover image

webmail

Flushing Fortran Output

Some 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.

  • NOTE: Do not open and close the file, or fflush, every time you output. your program will CRAWL, disk access is incredibly long, this is why the buffers are set high. Only do this at a point where output will be useful. If you do this too often, a program that normally takes 10 minutes, may take as long as a week, or more. I'm not kidding. Plus you will slow down others, so use this with caution and don't even think of putting it in a loop.

  • NOTE: For fortran this will only flush unit number 6, the standard output.

#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:
mordor> cc -c f77flush.c
mordor> f77 a.f f77flush.o
mordor> a.out >& a &

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.