Reading and Writing

7.1 Reading and Writing

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. 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:



  • A record containing a customer name (CUS-NAME) and the customer number (CUS-NUM) are read from a file customer.dat assign to IN-FILE.
  • The file is opened for INPUT (i.e. read-only).
  • The "prime read" refered to in the comment is the initial read of IN-FILE that allows for the possibility that the file contains no records.
  • The AT END clause tests for the end of file condition. When true, a series of statements can then be executed. Likewise, the NOT AT END clause allows for a series of statements to be executed when this condition is true. In the above example, when the file contains no more records (i.e. is at the end of the file) 'Y' is moved to EOF-FLAG, thereby making the condition name condition (END-OF-IN-FILE) true. When not at the end of the file, a record is read into memory and the paragraph PRINT-DETAILS is executed.
  • The statements between PERFORM UNTIL... and END-PERFORM are executed until the END-OF-IN-FILE condition is true (when the AT END of the read statement is true).

If you want to place data from a record into an item in WORKING-STORAGE (in addition to the memory space already allocated to the same data defined in the data division - so not much call for it), then use READ ... INTO. i.e:

    READ IN-FILE INTO W-RECORD-IN