FLUSH (Fortran 2003)
Purpose
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.
Syntax
- u
- is an integer scalar expression which has one of the
following values:
- A value in the range 1 through 2147483647
A NEWUNIT
value
- flush_list
- a list of specifiers that must contain UNIT=,
and can also contain one of each of the following specifiers:
- [UNIT=] specifies the external
file as an integer scalar expression which has one of the following
values:
- A value in the range 1 through 2147483647
A NEWUNIT
value
- ERR=stmt_label is an error specifier that specifies the statement label of an executable statement in the same scoping unit to which control is to transfer in the case of an error. Inclusion of the ERR= specifier suppresses error messages. stmt_label must be the statement label of a branch target statement that appears in the same scoping unit as the FLUSH statement.
- IOMSG=iomsg_variable is
an input/output status specifier that specifies the message returned
by the input/output operation. iomsg_variable is
a scalar default character variable. It must not be a use-associated
nonpointer protected variable. When the input/output statement containing
this specifier finishes execution, iomsg_variable is
defined as follows:
- If an error, end-of-file, or end-of-record condition occurs, the variable is assigned an explanatory message as if by assignment.
- If no such condition occurs, the value of the variable is unchanged.
- IOSTAT=ios specifies
the status of the flush operation as a scalar variable of type INTEGER.
When execution of the flush statement completes, ios is:
- A zero value if no error condition occurs.
- A positive value if an error occurs.
- A negative value if the device is not seekable such as a tape or TTY and the most recent data transfer operation was input.
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.
- [UNIT=] specifies the external
file as an integer scalar expression which has one of the following
values:
Rules
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.
Examples
Example 1:
In
the following example a data file written by a Fortran program is
read by a C routine. The program specifies a FLUSH statement
for the buffered I/O.
! 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]);
}
}



