Using the end-of-file condition (AT END)
You code the AT END
phrase
of the READ
statement
to handle errors or normal conditions, according to your program design.
At end-of-file, the AT END
phrase is performed. If
you do not code an AT END
phrase, the associated ERROR
declarative
is performed.
About this task
In many designs, reading sequentially to the end of a file is done intentionally, and the
AT END
condition is expected. For example, suppose you are processing a file that
contains transactions in order to update a main file:
PERFORM UNTIL TRANSACTION-EOF = "TRUE"
READ UPDATE-TRANSACTION-FILE INTO WS-TRANSACTION-RECORD
AT END
DISPLAY "END OF TRANSACTION UPDATE FILE REACHED"
MOVE "TRUE" TO TRANSACTION-EOF
END READ
. . .
END-PERFORM
Any NOT AT END
phrase is performed
only if the READ
statement completes successfully.
If the READ
operation fails because of a condition
other than end-of-file, neither the AT END
nor the NOT
AT END
phrase is performed. Instead, control passes to the
end of the READ
statement after any associated declarative
procedure is performed.
You might choose not to code either
an AT END
phrase or an EXCEPTION
declarative
procedure, but to code a status key clause for the file instead. In
that case, control passes to the next sequential instruction after
the input or output statement that detected the end-of-file condition.
At that place, have some code that takes appropriate action.