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.
Opening and Closing a File
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:
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.
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-nameEffects of OPEN modes includes the following:
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-FILEAdding 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). 003000When 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. 003100A 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.
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
|

