In order to either read, alter or create a new file, we
must first open it (even if it doesn't even exist yet). In doing so, a open mode
must be defined.
A D V E R T I S E M E N T
To simply read data from an existing file it would be opened in
INPUT mode. In this mode, the file is read-only and cannot be altered in any
way.
If writing to new file, i.e. creating one (or overwriting an existing file so be
careful) the new file would be opened in OUTPUT mode. You cannot read data from
a file opened in OUTPUT mode.
EXTEND mode allows for records to be added to the end of an existing file.
I-O mode is for input and output access to the file, such as when you wish to
update a record, or delete a record.
When a file is no longer required, the file needs to be closed again (using
CLOSE). You can open and close a file as often as you like during a program run,
although bear in mind that each time you open a file the computer will read from
the first record onwards (in INPUT and I-O mode) or will overwrite in OUTPUT
mode.
OPEN {INPUT or OUTPUT or I-O or EXTEND} {filename-1}...
{INPUT or OUTPUT or I-O or EXTEND} {filename-2}...
e.g.
OPEN INPUT DATA-1-FILE DATA-2-FILE
OUTPUT NEW-DATA-FILE
CLOSE DATA-1-FILE DATA-2-FILE NEW-DATA-FILE
READ
The READ statement will read the data from a file, taking precisely the data
that is defined in the file descriptor (FD) in the data division (file section)
(see The Four Divisions
section).
The format is:
READ {FD filename}
AT END {statements}
NOT AT END {statements}
END-READ
Since a file would likely contain more than one record, the READ statement is
often contained within a PERFORM loop: