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:

Syntax

ENDFILE ([UNIT=]io-unit [, ERR=label] [, 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.

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.

Rules and Behavior

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 the /vms compiler option and an ENDFILE is performed on a sequential unit, an actual one byte record containing a Ctrl/Z is 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 function to be executed, because unpredictable results can occur.

Compatibility

CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB

See Also: BACKSPACE, REWIND, Data Transfer I/O Statements, Branch Specifiers

Examples

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