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

Read syntax diagramSkip visual syntax diagram
>>-FLUSH--+-u----------+---------------------------------------><
          '-flush_list-'   

u
is an integer scalar expression which has one of the following values: This unit references an external file. The value of the integer scalar expression must not be an asterisk or a Hollerith constant.
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: The value of the integer scalar expression must not be an asterisk or a Hollerith constant.
  • 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.

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: The following example includes a Fortran program and a C routine. The program includes a FLUSH statement to read the input correctly.
! 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:

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]);
  }
}

Related information