The FLUSH statement makes data written to an external file available to other processes, or causes data placed in an external file by means other than Fortran to be available to a READ statement.
Inclusion of the IOSTAT specifier suppresses error messages. If the program encounters a severe error, the value of ios is 200.
If you do not specify ERR or IOSTAT, the program terminates on encountering a severe error.
The FLUSH statement must not appear in a pure subprogram.
A FLUSH statement has no effect on file position.
The buffering run-time option does not affect the execution of the FLUSH statement.
! The following Fortran program reads in data from standard input.
program main
integer, parameter :: N=11
integer :: rdat(N) /N * -1/
read(5, *) rdat(1:5)
FLUSH(5) ! Flush the input buffer
call csub()
read(5, *) rdat(6:10)
print *, rdat
end program
/* The following C routine reads in data. */
#include <stdio.h>
void csub() {
int dat5;
setbuf(stdin, NULL); /* specifies no buffering for stdin */
fscanf(stdin, "%d", &dat5);
printf("csub: dat5=%d\n", dat5);
}
Sample input file: 1 2 3 4 5
6
7 8 9 10 11
Execution: a.out < infile
Sample output: csub: dat5=6
1 2 3 4 5 7 8 9 10 11 -1
Example 2:
! The following Fortran program writes data to an external file.
subroutine process_data()
integer data(10)
external read_data
data = (/(i,i=1,10)/)
open(50, file="data_file")
write(50, *) data ! write data to an external file
flush(50) ! since Fortran I/O is buffered, a FLUSH
! statement is needed for the C routine to
! to read the data
call read_data(10) ! call C routine to read the file
end subroutine
/* The following C routine reads data from the external file. */
void read_data(int *sz) {
#include < stdio.h>
#include < stdlib.h>
int *data, i;
FILE *fp;
data = (int *) malloc((*sz)*sizeof(int));
fp = fopen("data_file", "r");
for (i=0; i<*sz-1; i++) {
fscanf(fp, "%d", &dat5[i]);
}
}