# ENDFILE

Statement: For sequential files, writes an end-of-file record to the file and positions the file after this record (the terminal point). For direct access files, truncates the file after the current record.

It can have either of the following forms:

ENDFILE ([UNIT=] io-unit[, ERR=label] [, IOMSG=msg-var] [, IOSTAT=i-var])

ENDFILE io-unit

io-unit | (Input) Is an external unit specifier.
label | Is the label of the branch target statement that receives control if an error occurs.
msg-var | (Output) Is a scalar default character variable that is assigned an explanatory message if an I/O error occurs.
i-var | (Output) Is a scalar integer variable that is defined as a positive integer if an error occurs and zero if no error occurs.

## Description

If the unit specified in the ENDFILE statement is not open, the default file is opened for unformatted output.

An end-of-file record can be written only to files with sequential organization that are accessed as formatted-sequential or unformatted-segmented sequential files. An ENDFILE performed on a direct access file always truncates the file.

End-of-file records should not be written in files that are read by programs written in a language other than Fortran.

### Note

If you use compiler option vms and an ENDFILE is performed on a sequential unit, an actual one byte record containing a CTRL+Zis written to the file. If this option is not specified, an internal ENDFILE flag is set and the file is truncated. The option does not affect ENDFILE on relative files; such files are truncated.

If a parameter of the ENDFILE statement is an expression that calls a function, that function must not cause an I/O statement or the EOF intrinsic functionto be executed, because unpredictable results can occur.

## Example

The following statement writes an end-of-file record to I/O unit 2:

      ENDFILE 2

Suppose the following statement is specified:

      ENDFILE (UNIT=9, IOSTAT=IOS, ERR=10)

An end-of-file record is written to the file connected to unit 9. If an error occurs, control is transferred to the statement labeled 10, and a positive integer is stored in variable `IOS`.

The following shows another example:

      WRITE (6, *) x
      ENDFILE 6
      REWIND 6
      READ (6, *) y

Parent topic: E to F

## See Also

BACKSPACE
REWIND
Data Transfer I/O Statements
Branch Specifiers