Teach Yourself COBOL in 21 days, Second Edition

Previous chapterNext chapterContents


- Day 9 -
File I/O

Welcome to the big time! Saving data to and retrieving data from a disk depends on you being able to handle files. The primary activity of COBOL programs is storage, retrieval, sorting, and reporting on files of information. Today, you learn about the following topics:

What Is a File?

Imagine a small company that keeps two card file boxes. One box contains a card for each vendor who supplies raw material to the company. The other contains a card for each customer who has purchased from the company. Close both boxes, label one Vendors and the other Customers, and bring a stranger in to look at the boxes.

The stranger would see two labeled 3x5 inch boxes that take up space on the desk. The stranger might assume that the boxes contain data on customers and vendors, but the boxes could contain half a tuna fish sandwich and the boss's lucky golf ball.

On a computer disk, a file is similar to a card file box. It has a label (the filename on the disk) and takes up space on the disk. The computer has no idea what kind of data is in a file; the file is just a chunk of disk space set aside for data and given a name. The file could be a word processing document file named james.doc containing a letter written to James, or it could be an Excel spreadsheet named bal.xls containing account balances. These are sometimes called physical files.

Back in our hypothetical office, the stranger might open the boxes to check their contents, and he would see that the physical card file box contains a collection of data.

New Term: In a COBOL program, a file is a collection of related units of information within a data category. A file might contain all the information (related units of information) about customers (a data category) for a company. This usually is called a data file or a logical file.

For the file to exist, there must be a physical file on the disk. When the bytes in this file are arranged logically so that a COBOL program can access the information, it becomes a data file to a COBOL program.

What Is a Record?

In terms of the Customer card file analogy, each card contains the information about a single customer, and the card file box itself is the physical file.

New Term: Within a data file, the information about one unit is called a record. If a data file contains the information pertaining to all customers, for example, the information about one customer is a record.

Figure 9.1 shows the relationship of a card file of phone numbers to a file record and fields. Each card contains a last name, a first name, and a phone number.

Figure 9.1.
A card file as a file, records, and fields.

What Is a Field?

In the customer file, the customer name is one field and the customer phone number is another field. In the card file analogy, you might place the customer's name on the first line of the card and the phone number on the second line. The first line, then, is the name field and the second line is the phone number field.

New Term: A field or data field is one piece of data contained in a record.

COBOL data files are organized as one or more records containing the same fields in each record. For example, a record for a personal phone book might contain fields for a last name, a first name, and a phone number. These fields would exist in each individual record.

If you displayed the phone file, it might look like the one in Figure 9.2.

Figure 9.2.
The contents of a phone file.

In fact, this collection of data is arranged in fields, similar to the card file system, but the layout is slightly different (as shown in Figure 9.3).

Figure 9.3.
File, record, and fields in a data file.

If you could lay a column ruler down over the records as they are displayed, you would see the starting and ending position of each of the fields (as shown in Figure 9.4).

Figure 9.4.
A column ruler against the records.

A record layout for the phone record would include a data name and the starting and ending positions in the record for each field (as shown in Figure 9.5).

Figure 9.5.
A customer record layout.

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

Table 9.1. Alternate record layout for a phone record.

Field Starting Position Length Type of Data
LAST NAME 1 20 Alphanumeric
FIRST NAME 21 20 Alphanumeric
PHONE 41 15 Alphanumeric

Defining a File in COBOL

In a COBOL program, the description of a data record is entered as a structure variable. Listing 9.1 shows a COBOL description for the PHONE-RECORD.

TYPE: Listing 9.1. The PHONE-RECORD structure.

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

ANALYSIS: When dealing with files, all the variables associated with a file, including the variables in the record, start with an identical prefix. In Listing 9.1, I chose PHONE- as the prefix, although PH- or PHNBK- could have been used as well. The prefix should not be too long because it will appear at the beginning of all variables. PHONE-BOOK- would have been okay, but it is a little long. The prefix convention is a good one, but it is not a requirement of the COBOL language.

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.

The Logical Description of a COBOL File

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. Listing 9.2 shows an example of a logical file definition, which is correctly placed in the FILE SECTION of the DATA DIVISION. FILE SECTION is a reserved name for the section of the DATA DIVISION used to define logical files used in a program.

TYPE: Listing 9.2. A logical 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

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

New Term: A logical file definition is called a file descriptor, or FD.

The file descriptor starts at line 002400 and is given the special level number FD. This appears in Area A, followed by a variable name for the logical file in Area B (columns 12 through 72). In this case, the variable name is PHONE-FILE.

The variable name used for a file does not truly name a variable; it really is a file identifier. You cannot move anything to PHONE-FILE, and if you tried to, the compiler probably would generate an error. You do need to open and close a file in order to be able to read data from it and write data to it, and the filename identifier is used with the OPEN and CLOSE commands to open and close the file. (See the section "Opening and Closing a File," later in this chapter.)

A file descriptor frequently is called an FD because it uses the letters FD as a level number. The FD for the PHONE-FILE continues on line 002500 with the statement LABEL RECORDS ARE STANDARD and ends with a period on that line.

The LABEL RECORDS ARE STANDARD clause is a holdover from the days of processing files on tape. Before disk drives became inexpensive, most large data files were written to and read from tape drives. There were two ways to store a file on a tape: labeled and unlabeled. The FD indicated which type of labeling was used on the tape.

All files are labeled so they have a name on the disk. Any file to be processed on a disk drive should include LABEL RECORDS ARE STANDARD. Some recent COBOL compilers have recognized that much more processing these days is done on disk, and the LABEL RECORDS clause is optional. You should include the clause to keep your COBOL compatible across different machines and COBOL compilers.

After the FD, filename, and LABEL clause, the variable structure that describes the record begins at line 002600. The variable structure that describes a record is often simply called a record and is a level 01 variable structure with the individual variables defined under it.

The Physical Description of a COBOL File

The physical description of the file fits in the ENVIRONMENT DIVISION.

New Term: 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 ENVIRONMENT DIVISION is reserved to provide information on the physical computer on which COBOL is running. Different types of computers have different ways of naming files on a disk, so it makes sense that this information is placed in the ENVIRONMENT DIVISION. A physical file description is placed in the FILE-CONTROL paragraph in the INPUT-OUTPUT SECTION of the ENVIRONMENT DIVISION.

The INPUT-OUTPUT SECTION is a reserved name for the section of the ENVIRONMENT DIVISION that is used to define physical files used in a program and to define which areas of memory will be used by the files while they are being processed.

The FILE-CONTROL paragraph is a reserved name for a paragraph in the INPUT-OUTPUT SECTION of the ENVIRONMENT DIVISION, and it is used to define all the physical files used in a program.

Listing 9.3 shows an example of a physical file description. The INPUT-OUTPUT SECTION at line 000500 begins in Area A. The FILE-CONTROL paragraph at line 000600 also begins in Area A.

TYPE: Listing 9.3. 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.

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

The last line of the SELECT clause at line 000900 specifies that the file organization is SEQUENTIAL. SEQUENTIAL organization indicates that records in the file are processed in sequence. When a record is written to the file, it always is appended to the end of the file. When a file is opened and read, the first read retrieves the first record and each subsequent read retrieves each next record.

If you think of this in terms of the card file of names and phone numbers, it means that every new card file is added at the back end of the card file box, and you must find a card by reading from the first card in the box to the last.


NOTE: The SELECT clause in Listing 9.3 is suitable for MS-DOS machines. On some machines, it might be necessary to omit the extension and use ASSIGN TO "phone" or force the filename to uppercase and use ASSIGN TO "PHONE".

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

These are examples of the four OPEN modes, and Table 9.2 describes what happens to the file in each one:

OPEN OUTPUT PHONE-FILE
OPEN EXTEND PHONE-FILE
OPEN INPUT PHONE-FILE
OPEN I-O PHONE-FILE

Table 9.2. Effects of OPEN modes.

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.

Notice that files opened for INPUT or I-O can cause an error if the file does not physically exist on the disk.

Some versions of COBOL allow you to add the reserved word OPTIONAL to a SELECT clause to prevent these errors. (See Listing 9.4.) The word OPTIONAL indicates that if the file is opened in INPUT or I-O mode, the file is created automatically if it does not exist. If your version of COBOL supports OPTIONAL, use it. Micro Focus Personal COBOL, ACUCOBOL, LPI COBOL, and VAX COBOL all support it.

TYPE: Listing 9.4. SELECT OPTIONAL.

000300
000400 ENVIRONMENT DIVISION.
000500 INPUT-OUTPUT SECTION.
000600 FILE-CONTROL.
000700     SELECT OPTIONAL PHONE-FILE
000800         ASSIGN TO "phone.dat"
000900         ORGANIZATION IS SEQUENTIAL.
001000
001100 DATA DIVISION.

Closing a file is a lot less complicated; you simply use the reserved word CLOSE. It doesn't matter what the original open mode was. Here is an example:

CLOSE filename

For the last listing, you would use the following:

CLOSE PHONE-FILE

Adding Records to a File

Adding a record to a file involves writing a record. The following syntax is simple:

WRITE file-record

To continue the phone number file example, use the following:

WRITE PHONE-RECORD

In Listing 9.5, phnadd01.cbl, all the elements of the program should be recognizable to you. The user is repeatedly asked for a last name, a first name, and a phone number. Study phnadd01.cbl; then code it, compile it, and run it to create your own phone file. All your entries are written to the file one after the other. In this listing, you open a new file and only add records to it. Therefore, opening in EXTEND mode is suitable.

TYPE: Listing 9.5. Creating a file and adding records.

000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. PHNADD01.
000300*--------------------------------------------------
000400* This program creates a new data file if necessary
000500* and adds records to the file from user entered
000600* data.
000700*--------------------------------------------------
000800 ENVIRONMENT DIVISION.
000900 INPUT-OUTPUT SECTION.
001000 FILE-CONTROL.
001100     SELECT OPTIONAL PHONE-FILE
001200*or  SELECT PHONE-FILE
001300         ASSIGN TO "phone.dat"
001400*or      ASSIGN TO "phone"
001500         ORGANIZATION IS SEQUENTIAL.
001600
001700 DATA DIVISION.
001800 FILE SECTION.
001900 FD  PHONE-FILE
002000     LABEL RECORDS ARE STANDARD.
002100 01  PHONE-RECORD.
002200     05  PHONE-LAST-NAME      PIC X(20).
002300     05  PHONE-FIRST-NAME     PIC X(20).
002400     05  PHONE-NUMBER         PIC X(15).
002500
002600 WORKING-STORAGE SECTION.
002700
002800* Variables for SCREEN ENTRY
002900 01  PROMPT-1               PIC X(9) VALUE "Last Name".
003000 01  PROMPT-2               PIC X(10) VALUE "First Name".
003100 01  PROMPT-3               PIC X(6) VALUE "Number".
003200
003300 01  YES-NO                 PIC X.
003400 01  ENTRY-OK               PIC X.
003500
003600 PROCEDURE DIVISION.
003700 MAIN-LOGIC SECTION.
003800 PROGRAM-BEGIN.
003900
004000     PERFORM OPENING-PROCEDURE.
004100     MOVE "Y" TO YES-NO.
004200     PERFORM ADD-RECORDS
004300         UNTIL YES-NO = "N".
004400     PERFORM CLOSING-PROCEDURE.
004500
004600 PROGRAM-DONE.
004700     STOP RUN.
004800
004900* OPENING AND CLOSING
005000
005100 OPENING-PROCEDURE.
005200     OPEN EXTEND PHONE-FILE.
005300
005400 CLOSING-PROCEDURE.
005500     CLOSE PHONE-FILE.
005600
005700 ADD-RECORDS.
005800     MOVE "N" TO ENTRY-OK.
005900     PERFORM GET-FIELDS
006000        UNTIL ENTRY-OK = "Y".
006100     PERFORM ADD-THIS-RECORD.
006200     PERFORM GO-AGAIN.
006300
006400 GET-FIELDS.
006500     MOVE SPACE TO PHONE-RECORD.
006600     DISPLAY PROMPT-1 " ? ".
006700     ACCEPT PHONE-LAST-NAME.
006800     DISPLAY PROMPT-2 " ? ".
006900     ACCEPT PHONE-FIRST-NAME.
007000     DISPLAY PROMPT-3 " ? ".
007100     ACCEPT PHONE-NUMBER.
007200     PERFORM VALIDATE-FIELDS.
007300
007400 VALIDATE-FIELDS.
007500     MOVE "Y" TO ENTRY-OK.
007600     IF PHONE-LAST-NAME = SPACE
007700         DISPLAY "LAST NAME MUST BE ENTERED"
007800         MOVE "N" TO ENTRY-OK.
007900
008000 ADD-THIS-RECORD.
008100     WRITE PHONE-RECORD.
008200
008300 GO-AGAIN.
008400     DISPLAY "GO AGAIN?".
008500     ACCEPT YES-NO.
008600     IF YES-NO = "y"
008700         MOVE "Y" TO YES-NO.
008800     IF YES-NO NOT = "Y"
008900         MOVE "N" TO YES-NO.
009000

The following shows the output of phnadd01.cbl after two entries:

OUTPUT:

Last Name  ?
KARENINA
First Name ?
ANA
Number     ?
 (818) 555-4567
GO AGAIN?
Y
Last Name  ?
PENCIL
First Name ?
ARTHUR
Number     ?
 (515) 555-1234
GO AGAIN?

ANALYSIS: The listing includes some of the options for the SELECT statement at lines 001200 and 001400. The prompts used to ask the user for input are defined at lines 002900 through 003100.

At line 004000, OPENING-PROCEDURE is performed as the first step of the program. At line 004400, CLOSING-PROCEDURE is performed as the last step of the program. These two paragraphs at lines 005100 and 005400 open and close the file. It is a fairly common arrangement for the first step of a program to open all the files required by the program and for the last step of the program to close all open files.

The ADD-RECORDS loop, at lines 005700 through 006200, asks the user for all the fields UNTIL ENTRY-OK = "Y", and then writes the record to the file.

At line 006400, the GET-FIELDS paragraph initializes the PHONE-RECORD to spaces and then asks the user for each of the three fields. The entries are validated (checked for correct entry) in VALIDATE-FIELDS. The only validation is to check that the last name is entered.

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. Physical files, logical files, and how they are described in COBOL programs are covered in more detail in Day 17, "Alternate Keys."


DO/DON'T:
DO
ensure that if more than one program accesses the same file, both programs use the same SELECT, and an FD of the same length to describe the file.

DON'T try to open a file with a SELECT or FD that is different from the SELECT or FD used to create the file. There are special cases where you can do this, but they are used in data conversions.

DON'T change the record layout of a file by adding fields that change the length of the record without also re-creating the data file.


A file to be opened for reading should be opened in INPUT mode with the following syntax:

READ filename NEXT RECORD.

Here is an example:

READ PHONE-FILE NEXT RECORD.

Because the SELECT clause for the file says that it is organized as a SEQUENTIAL file, every READ on the file is a request to retrieve the next record. Each READ reads the next record in the file. There actually are two types of read for a file: READ and READ NEXT. Because a SEQUENTIAL file is organized to be accessed one record after another, a READ and READ NEXT have the same effect on a SEQUENTIAL file. There are other types of file organization that you will begin to explore in Day 11, "Indexed File I/O." For these other file organizations, READ and READ NEXT do not have the same effect.

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]

The following is an example:

READ PHONE-FILE
READ PHONE-FILE NEXT RECORD


NOTE: For a SEQUENTIAL file, READ and READ NEXT RECORD are identical.

You need to know one other thing about READ NEXT. How do you know when all the records in a file have been read? If you don't find out, your program will keep attempting to read the next record when nothing is left in the file to read. The READ NEXT statement can include a clause that is executed if the file has reached the end. The following is the syntax for handling an AT END condition:

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

Listing 9.6, phnlst01.cbl, lists all the records in your data file. Before you start looking over the code, take a look at the output of phnlst01.cbl.

TYPE: Listing 9.6. Displaying the phone file.

000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. PHNLST01.
000300*--------------------------------------------------
000400* This program displays the contents of the
000500* phone file.
000600*--------------------------------------------------
000700 ENVIRONMENT DIVISION.
000800 INPUT-OUTPUT SECTION.
000900 FILE-CONTROL.
001000     SELECT OPTIONAL PHONE-FILE
001100*or  SELECT PHONE-FILE
001200         ASSIGN TO "phone.dat"
001300*or      ASSIGN TO "phone"
001400         ORGANIZATION IS SEQUENTIAL.
001500
001600 DATA DIVISION.
001700 FILE SECTION.
001800 FD  PHONE-FILE
001900     LABEL RECORDS ARE STANDARD.
002000 01  PHONE-RECORD.
002100     05  PHONE-LAST-NAME      PIC X(20).
002200     05  PHONE-FIRST-NAME     PIC X(20).
002300     05  PHONE-NUMBER         PIC X(15).
002400
002500 WORKING-STORAGE SECTION.
002600
002700* Structure for SCREEN DISPLAY
002800 01  FIELDS-TO-DISPLAY.
002900     05  PROMPT-1             PIC X(10) VALUE "Last Name:".
003000     05  DISPLAY-LAST-NAME    PIC X(20).
003100     05  PROMPT-2             PIC X(6) VALUE "First:".
003200     05  DISPLAY-FIRST-NAME   PIC X(20).
003300     05  PROMPT-3             PIC X(3) VALUE "NO:".
003400     05  DISPLAY-NUMBER       PIC X(15).
003500
003600 01  END-OF-FILE            PIC X.
003700
003800 01  SCREEN-LINES           PIC 99.
003900 01  A-DUMMY                PIC X.
004000
004100 PROCEDURE DIVISION.
004200 MAIN-LOGIC SECTION.
004300 PROGRAM-BEGIN.
004400
004500     PERFORM OPENING-PROCEDURE.
004600     MOVE ZEROES TO SCREEN-LINES.
004700     MOVE "N" TO END-OF-FILE.
004800     PERFORM READ-NEXT-RECORD.
004900     PERFORM DISPLAY-RECORDS
005000         UNTIL END-OF-FILE = "Y".
005100     PERFORM CLOSING-PROCEDURE.
005200
005300 PROGRAM-DONE.
005400     STOP RUN.
005500
005600 OPENING-PROCEDURE.
005700     OPEN INPUT PHONE-FILE.
005800
005900 CLOSING-PROCEDURE.
006000     CLOSE PHONE-FILE.
006100
006200 DISPLAY-RECORDS.
006300     PERFORM DISPLAY-FIELDS.
006400     PERFORM READ-NEXT-RECORD.
006500
006600 DISPLAY-FIELDS.
006700     IF SCREEN-LINES = 15
006800         PERFORM PRESS-ENTER.
006900     MOVE PHONE-LAST-NAME TO DISPLAY-LAST-NAME.
007000     MOVE PHONE-FIRST-NAME TO DISPLAY-FIRST-NAME.
007100     MOVE PHONE-NUMBER TO DISPLAY-NUMBER.
007200     DISPLAY FIELDS-TO-DISPLAY.
007300
007400     ADD 1 TO SCREEN-LINES.
007500
007600 READ-NEXT-RECORD.
007700     READ PHONE-FILE NEXT RECORD
007800        AT END
007900        MOVE "Y" TO END-OF-FILE.
008000
008100 PRESS-ENTER.
008200     DISPLAY "Press ENTER to continue . . ".
008300     ACCEPT A-DUMMY.
008400     MOVE ZEROES TO SCREEN-LINES.
008500

OUTPUT:

C>pcobrun phnlst01
Personal COBOL version 2.0 from Micro Focus
PCOBRUN V2.0.02  Copyright (C) 1983-1993 Micro Focus Ltd.
Last Name:KARENINA             First:ANITA	NO:(818) 555-4567
Last Name:PENCIL               First:ARTHUR	NO:(515) 555-1234
Last Name:BUDLONG              First:MO	NO:(818) 555-4444
Last Name:ATREIDES             First:JESSICA	NO:606-555-7777
Last Name:CORTEZ               First:HERNAN	NO:555-4567
Last Name:WAYNE                First:BOB	NO:555-4332
Last Name:ADALE                First:ALLEN	NO:415-555-6666
Last Name:NOTTINGHAM           First:MARY	NO:415-555-6789
Last Name:TUCK                 First:FRANCINE	NO:213-555-2345
Last Name:SCARLET              First:BILL	NO:202-555-6789
Last Name:PLUM                 First:PETRA	NO:202-555-5678
Last Name:RED                  First:ERIC	NO:424-555-3456
Last Name:ANGEL                First:DESTINY	NO:616-555-2345
Last Name:BACH                 First:JOHN	NO:555-6789
Last Name:BAGGINS              First:BILBO	NO:555-9876
Press ENTER to continue . .

ANALYSIS: The OPEN at line 005700 is in INPUT mode. After the open, an END-OF-FILE is set up and the first record is read at lines 004700 and 004800. The READ-NEXT-RECORD paragraph at line 007600 uses the AT END clause of a READ NEXT to set the END-OF-FILE flag to "Y" when the records in the file are exhausted.

The DISPLAY-RECORDS paragraph at line 006200 displays the fields and then performs the READ-NEXT-RECORD paragraph. The READ-NEXT-RECORD paragraph sets the END-OF-FILE flag, so DISPLAY-RECORDS UNTIL END-OF-FILE ="Y" works correctly to control the loop.

The DISPLAY-FIELDS paragraph at line 006600 includes stopping for the user to press Enter after every 15 lines of display.

COBOL programs frequently process a file from one end to the other. This is such a common processing problem that it's worth looking at the techniques used in this example to control the processing loop.

Listing 9.7 shows the READ NEXT logic from phnlst01.cbl. In any program that reads a file from one end to the other, it is important that the paragraph or command used to read the next record sets a flag when the file reaches the end. This flag is used elsewhere in the program to determine that the processing loop reading through the file must stop.

TYPE: Listing 9.7. READ-NEXT logic from phnlst01.cbl.

007600 READ-NEXT-RECORD.
007700     READ PHONE-FILE NEXT RECORD
007800        AT END
007900        MOVE "Y" TO END-OF-FILE.
008000


DO/DON'T:
DO
ensure that whenever a file is read sequentially using READ NEXT, some flag is set to indicate the end of a file.

DON'T ignore the possible end-of-file condition by leaving out a test for it in the main logic of the program.


The File Processing Loop

The phnlst01.cbl program includes a piece of coding that is important to understand when you are writing processing loops that involve reading through a file. Listing 9.8 repeats the two key parts of the program, but you might also want to refer to Listing 9.6.

TYPE: Listing 9.8. The control of the processing loop from phnlst01.cbl.

004700     MOVE "N" TO END-OF-FILE.
004800     PERFORM READ-NEXT-RECORD.
004900     PERFORM DISPLAY-RECORDS
005000         UNTIL END-OF-FILE = "Y".
......
......* The processing loop
006200 DISPLAY-RECORDS.
006300     PERFORM DISPLAY-FIELDS.
006400     PERFORM READ-NEXT-RECORD.
006500
......
......
007600 READ-NEXT-RECORD.
007700     READ PHONE-FILE NEXT RECORD
007800        AT END
007900        MOVE "Y" TO END-OF-FILE.
008000

ANALYSIS: At lines 004700 and 004800, the first record in the file is read before the processing loop starts. At lines 004900 and 005000, the processing loop is performed until the end of the file. At line 006400, the next record is read at the end of the processing loop.

The style of processing loop for handling a file illustrated by these three pieces of code is very effective and easy to code. You can think of it as three key pieces to use when processing a file from one end to the other:

1. Set a flag that keeps track of whether the file is at the end to reflect a "not at end" condition and read the first record.

2. Perform the processing loop until the file is at end.

3. Read the next record at the bottom of the processing loop.

Compare these three pieces of a file processing loop to the three parts of controlling any processing loop from Day 5, "Using PERFORM, GO TO, and IF to Control Programs," as shown in Table 9.3. You will see that a file processing loop is just a specialized version of a processing loop. The control variable is the "at end" flag for the file.

Table 9.3. Comparing a processing loop to a file processing loop.

Processing Loop Action File Processing Loop Action
1. Initialize a value for the first pass through the loop. 1. Set an "at end" flag to reflect a "not at end" condition and read the first record.
2. Perform the processing loop until a condition is met. 2. Perform the processing loop until the file ends.
3. Increment controlor modify the variable at the end of the loop or after each pass through the loop. 3. Read the next record at the end of the processing loop.

In fact, this comparison makes it possible to improve the description of the three parts of a processing loop, as in Table 9.4.

Table 9.4. An improved statement of processing loop design.

Processing Loop Action Comments
1. Attempt to set up all variables and conditions needed for the first pass through the loop. This includes setting up one or more loop control variables, and can include reading a record from a file.
2. Perform the processing loop until one or more conditions are met.
3. At the end of the loop, attempt to set up all conditions for the next pass through the loop. This includes modifying the loop control variables, and can include reading the next record.

The general description of a processing loop now covers earlier control loops that you have studied as well as the somewhat specialized control loop used to process a file one record at a time.

Summary

Saving data to and retrieving data from a disk depends on being able to handle files. Today, you learned the following basics:

Q&A

Q Can I print the contents of phone.dat?

A Yes. Day 10, "Printing," covers printing data files. Printing is one of the most common instances of processing a file by reading all the way through it one record at a time.

Q Is writing to a file the same as adding to a file?

A Yes. The WRITE command adds a new record to a file.

Q Why is the filename in PHNADD01.CBL (phone.dat) in lowercase?

A The physical filename you assign depends on the requirements of the computer on which the program will be compiled. Under MS-DOS, filenames always are converted to uppercase no matter what you use for the name. Using ASSIGN TO "phone.dat" or ASSIGN TO "PHONE.DAT" creates a file named PHONE.DAT. Other computers have other requirements. In UNIX, case is not converted, and phone.dat and PHONE.DAT are two different names for two different files. The examples given in phnadd01.cbl, phnlst01.cbl, and other programs in this chapter use lowercase because it is common to several different computers.

Workshop

Quiz

If you add a field to the PHONE-RECORD to include the extension and recompile the program, will the original file phone.dat process correctly using the new program?

Exercises

1. Copy phnadd01.cbl to phnadd02.cbl. Add a field to PHONE-RECORD to include a phone extension, and add all the logic needed to ask the user for this new field and store it in the file. Remember that your original data file, phone.dat, will not be processed correctly by this new file definition, and you will have to delete or rename the existing phone.dat file before you run phnadd02.cbl.

Hint: The new record layout is the following:
002100 01  PHONE-RECORD.
002200     05  PHONE-LAST-NAME      PIC X(20).
002300     05  PHONE-FIRST-NAME     PIC X(20).
002400     05  PHONE-NUMBER         PIC X(15).
002500     05  PHONE-EXTENSION      PIC X(5).
2. Copy phnlst01.cbl to phnlst02.cbl and modify it to display the additional extension field.

Hint: You will find that the result will not display correctly on an 80-column screen, and you will need to shorten the prompts so that everything can be squeezed into 80 columns. If you use prompts as suggested in the following, everything will fit:
002900 01  DISPLAY-FIELDS.
003000     05  PROMPT-1             PIC X(4) VALUE "Lst:".
003100     05  DISPLAY-LAST-NAME    PIC X(20).
003200     05  PROMPT-2             PIC X(4) VALUE "1st:".
003300     05  DISPLAY-FIRST-NAME   PIC X(20).
003400     05  PROMPT-3             PIC X(3) VALUE "NO:".
003500     05  DISPLAY-NUMBER       PIC X(15).
003600     05  PROMPT-4             PIC X(4) VALUE "Xtn:".
003700     05  DISPLAY-EXTENSION    PIC X(5).
003800
Smith               Michael Valentine   818-555-1212
Trent               Jack and Diane      555-9292
Kite                Alonso              (213)555-7777
Mae                 Maggie              506 555 1234
Karenina            Ana                 (415) 555-3333


Previous chapterNext chapterContents


© Copyright, Macmillan Computer Publishing. All rights reserved.