Cobol Files

Defining Record Structure in Cobol
An alternative method of creating a record specification is to list the field name, starting position, length, and data type for each field, as shown in Table below.

If you are familiar with databases such as dBASE IV, Oracle, Informix, or Access, you will recognize that this type of layout is similar to defining a database file.

Example below is a basic Telephone Record structure for a customer or for other purposes and as follows:
Field Starting Position Length Type of Data
LAST NAME 1 20 Alphanumeric
FIRST NAME 21 20 Alphanumeric
PHONE 41 15 Alphanumeric

In COBOL, a file is defined in two parts: the logical file, which includes the record layout, and the physical file, which includes the name of the file on the disk and how the file will be organized.

At line 002200, the FILE SECTION appears before the WORKING-STORAGE section in the DATA DIVISION. Because it is a section name, it starts in Area A (columns 8 through 11).

The physical description of the file fits in the ENVIRONMENT DIVISION.    The physical description of a COBOL file is a set of statements designed to identify the physical name of the file on the disk drive, and how the file is organized.

The SELECT statement that defines the physical file begins at line 000700.

The SELECT clause uses the logical filename (PHONE-FILE) and associates it with a physical filename on the disk by using the ASSIGN TO "phone.dat" at line 000800.

Opening and Closing a File
Before you can do anything to the contents of a file, you must open the file. When you open a file, you have to specify an OPEN mode. A mode indicates the type of processing that you intend to do on the file. The following syntax is used:
OPEN mode file-name
Effects of OPEN modes includes the following:
Open Mode Effect
OUTPUT If the file does not exist, it is created and records can be added to the file. If the file does exist, the old file is destroyed, a new one is created, and any new records can be added to it.
EXTEND If the file does not exist, it is created and records can be added to the file. If the file does exist, it is opened and records can be appended to the end of it.
INPUT If the file exists, it is opened and records can be read from the file. If the file does not exist, the OPEN fails and an error occurs.
I-O If the file exists, it is opened for reading and writing. If the file does not exist, the open fails and an error occurs.

Closing a file is a lot less complicated; you simply use the reserved word CLOSE. It does not matter what the original open mode was. Here is an example:
CLOSE PHONE-FILE
Adding a record to a file involves writing a record. The following syntax is simple:
WRITE file-record
The PHONE-RECORD structure
In a COBOL program, the description of a data record is entered as a structure variable.   See the sample code below:
002500
002600     01  PHONE-RECORD.
002700         05  PHONE-LAST-NAME      PIC X(20).
002800         05  PHONE-FIRST-NAME     PIC X(20).
002900         05  PHONE-NUMBER         PIC X(15).
003000
When dealing with files, all the variables associated with a file, including the variables in the record, start with an identical prefixed and here we used the keyword "PHONE".    Although PH- or PHNBK- could have been used as well and should not be too long.

Logical Description of a Cobol File
002000
002100 DATA DIVISION.
002200 FILE SECTION.
002300
002400 FD  PHONE-FILE
002500         LABEL RECORDS ARE STANDARD.
002600     01  PHONE-RECORD.
002700     05  PHONE-LAST-NAME      PIC X(20).
002800     05  PHONE-FIRST-NAME     PIC X(20).
002900     05  PHONE-NUMBER         PIC X(15).
003000
003100 WORKING-STORAGE SECTION.
003100
A logical file is easy to define after you define the record.    A logical file is defined in the DATA DIVISION, in a new section called the FILE SECTION.

The Physical Phone-File
000400 ENVIRONMENT DIVISION.
000500 INPUT-OUTPUT SECTION.
000600 FILE-CONTROL.
000700     SELECT PHONE-FILE
000800         ASSIGN TO "phone.dat"
000900         ORGANIZATION IS SEQUENTIAL.
001000
001100 DATA DIVISION.
Reading Records from a File
In order to open a file and read it successfully, the physical and logical file definition in the program (FD and SELECT) must match the FD and SELECT used to create the file, at least as far as record length. If they don't match, unpredictable results will occur.
A file to be opened for reading should be opened in INPUT mode with the following syntax:
READ filename NEXT RECORD.
READ PHONE-FILE NEXT RECORD.
Because the intention in a READ on a SEQUENTIAL file is to get the next record, it is a good idea to add NEXT to the READ statement, as shown in the following syntax, because it makes the intention clear:
READ filename [NEXT RECORD]
    AT END
    do something
For your phone number file, the following will work:
READ PHONE-FILE NEXT RECORD
    AT END
    MOVE "Y" TO END-OF-FILE