Compaq COBOL
User Manual


Previous Contents Index

Record Access Mode

The methods for retrieving and storing records in a file are called record access modes. Compaq COBOL supports the following three types of record access modes:

When you omit the ACCESS MODE IS clause in the SELECT statement, the access mode is sequential.

Example 6-17 shows sample SELECT statements for sequential files with sequential access modes.

Example 6-17 SELECT Statements for Sequential Files with Sequential Access Mode

                  (1)                                     (2) 
FILE-CONTROL.                              FILE-CONTROL. 
    SELECT LIST-FILE                           SELECT PAYROLL 
           ASSIGN TO "MAIL.LIS"                       ASSIGN TO "PAYROL.DAT". 
           ORGANIZATION IS SEQUENTIAL 
           ACCESS IS SEQUENTIAL. 

Sample SELECT statements for relative files with sequential and dynamic access modes are shown in Example 6-18.

Example 6-18 SELECT Statements for Relative Files with Sequential and Dynamic Access Modes

                  (1)                                     (2) 
FILE-CONTROL.                             FILE-CONTROL. 
    SELECT MODEL                              SELECT PARTS 
           ASSIGN TO "ACTOR.DAT"                     ASSIGN TO "PART.DAT" 
           ORGANIZATION IS RELATIVE                  ORGANIZATION IS RELATIVE 
           ACCESS MODE IS SEQUENTIAL.                ACCESS MODE IS DYNAMIC 
                                                     RELATIVE KEY IS PART-NO. 

Sample SELECT statements for indexed files with dynamic and sequential access modes are shown in Example 6-19.

Example 6-19 SELECT Statements for Indexed Files with Dynamic and Default Sequential Access Modes

                  (1)                                     (2) 
FILE-CONTROL.                                FILE-CONTROL. 
    SELECT A-GROUP                               SELECT TEAS 
           ASSIGN TO "RFCBA.PRO"                        ASSIGN TO "TEA" 
           ORGANIZATION IS INDEXED                      ORGANIZATION IS INDEXED 
           ACCESS MODE IS DYNAMIC                       RECORD KEY IS LEAVES. 
           RECORD KEY IS WRITER 
           ALTERNATE RECORD KEY IS EDITOR. 

Because the default file organization is also sequential, both the relative and indexed examples require the ORGANIZATION IS clause.

Sample SELECT statements for line sequential files with sequential access modes are shown in Example 6-20.

Example 6-20 SELECT Statements for Line Sequential Files with Sequential Access Modes (Alpha)

          (1)                                          (2) 
FILE-CONTROL.                                FILE-CONTROL.  
    SELECT MAMMALS                               SELECT VACATION-SPOTS 
           ASSIGN TO "DOLPHINS"                         ASSIGN TO "BAHAMAS" 
           ORGANIZATION IS LINE SEQUENTIAL              ORGANIZATION IS LINE SEQUENTIAL. 
           ACCESS MODE IS SEQUENTIAL.                               

6.3 Creating and Processing Files

Creating and processing sequential, line sequential, relative, and indexed files includes the following tasks:

  1. Opening the file
  2. Executing valid I/O statements
  3. Closing the file

Sections 6.3.2, 6.3.3, and 6.3.4 describe the specific tasks involved in creating and processing sequential, relative, and indexed files.

6.3.1 Opening and Closing Files

A Compaq COBOL program must open a file with an OPEN statement before any other I/O or Report Writer statement can reference it. Files can be opened more than once in the same program as long as they are closed before being reopened.

Sample OPEN and CLOSE statements are shown in Example 6-21.

Example 6-21 OPEN and CLOSE Statements

   .
   .
   .
OPEN INPUT MASTER-FILE. 
OPEN OUTPUT REPORT-FILE. 
OPEN I-O   MASTER-FILE2 
           TRANS-FILE 
     OUTPUT REPORT-FILE2. 
CLOSE MASTER-FILE. 
CLOSE TRANS-FILE, MASTER-FILE2 
      REPORT-FILE, REPORT-FILE2. 
   .
   .
   .

The OPEN statement must specify one of the following four open modes:

INPUT
OUTPUT
I-O {Not for LINE SEQUENTIAL}
EXTEND

Your choice, along with the file's organization and access mode, determines which I/O statements you can use. Sections 6.3.2, 6.3.3, and 6.3.4 discuss the I/O statements for sequential, relative, and indexed files, respectively. Section 12.8.4, Case Sensitivity on Tru64 UNIX explains the importance of attention to case.

When your program performs an OPEN statement, the following events take place:

  1. The I/O system builds a file specification by using the contents of the VALUE OF ID clause, if any, to alter or complete the file specification in the ASSIGN clause. Logicals and environment variables are translated.
  2. The I/O system checks the file's current status. If the file is unavailable, or if it was closed WITH LOCK, the OPEN statement fails. (See Chapter 8 for information on file sharing.)
  3. If the file specification names an invalid device, or contains any other errors, the I/O system generates an error message and the OPEN statement fails.
  4. The I/O system takes one of the following actions if it cannot find the file:
    1. If the file's OPEN mode is OUTPUT, the file is created.
    2. If the file's OPEN mode is EXTEND, or I-O, the OPEN statement fails, unless the file's SELECT clause includes the OPTIONAL phrase. If the file's SELECT clause includes the OPTIONAL phrase, the file is created.
    3. If the file's OPEN mode is INPUT, and its SELECT clause includes the OPTIONAL phrase, the OPEN statement is successful. The first read on that file causes the AT END or INVALID KEY condition.
    4. If none of the previous conditions is met, the OPEN fails and the Declarative USE procedure (if any) gains control. If no Declarative USE procedure exists, the I/O system aborts the program.
  5. If the file's OPEN mode is OUTPUT, and a file by the same name already exists, a new version is created.
  6. If the file characteristics specified by the program attempting an OPEN operation differ from the characteristics specified when the file was created, the OPEN statement fails.

If the file is on magnetic tape, the I/O system rewinds the tape. (To close a file on tape without rewinding the tape, use the NO REWIND phrase.) This speeds processing when you want to write another file beyond the end of the first file, as in the following example:


CLOSE MASTER-FILE NO REWIND. 

You can also close a file and prevent your program from opening that file again in the same run, as in the following example:


CLOSE MASTER-FILE WITH LOCK. 

6.3.2 File Handling for Sequential and Line Sequential (Alpha) Files

Creating a sequential or (on Alpha only) line sequential file involves the following:

  1. Opening the file for OUTPUT or EXTEND
  2. Executing valid I/O statements
  3. Closing the file

By default, Compaq COBOL assumes sequential organization and sequential access mode. (See Example 6-22.)

Example 6-22 Creating a Sequential File

IDENTIFICATION DIVISION. 
PROGRAM-ID. SEQ01. 
ENVIRONMENT DIVISION. 
INPUT-OUTPUT SECTION. 
FILE-CONTROL. 
    SELECT TRANS-FILE ASSIGN TO "TRANS.DAT". 
DATA DIVISION. 
FILE SECTION. 
FD  TRANS-FILE. 
01  TRANSACTION-RECORD    PIC X(25). 
PROCEDURE DIVISION. 
A000-BEGIN. 
    OPEN OUTPUT TRANS-FILE. 
    PERFORM A010-PROCESS-TRANS 
       UNTIL TRANSACTION-RECORD = "END". 
    CLOSE TRANS-FILE. 
    STOP RUN. 
A010-PROCESS-TRANS. 
    DISPLAY "Enter next record  - X(25)". 
    DISPLAY "enter END to terminate the session". 
    DISPLAY "-------------------------". 
    ACCEPT TRANSACTION-RECORD. 
    IF TRANSACTION-RECORD NOT = "END" 
       WRITE TRANSACTION-RECORD. 

Example 6-23 Creating a Line Sequential File (Alpha)

        IDENTIFICATION DIVISION. 
        PROGRAM-ID. LINESEQ01. 
        ENVIRONMENT DIVISION. 
        INPUT-OUTPUT SECTION. 
        FILE-CONTROL. 
            SELECT LINESEQ-FILE ASSIGN TO "LINESEQ.DAT". 
        DATA DIVISION. 
        FILE SECTION. 
        FD  LINESEQ-FILE. 
        01  LINESEQ-RECORD    PIC X(25). 
 
        PROCEDURE DIVISION. 
        A000-BEGIN. 
            OPEN OUTPUT LINESEQ-FILE. 
            CLOSE LINESEQ-FILE. 
            STOP RUN. 
 

By default, Compaq COBOL assumes sequential access mode when the line sequential organization is specified. (See Example 6-23.) <>

Statements for Sequential and Line Sequential (Alpha) File Processing

Processing a sequential file or line sequential file (Alpha) involves the following:

  1. Opening the file
  2. Processing the file with valid I/O statements
  3. Closing the file

Table 6-3 lists the valid I/O statements for sequential files, and Table 6-4 lists the valid I/O statements for line sequential files. Both tables illustrate the following relationships:

Table 6-3 Valid I/O Statements for Sequential Files
      Open Mode
File
Organization
Access
Mode
Statement INPUT OUTPUT I/O EXTEND
SEQUENTIAL SEQUENTIAL READ Yes No Yes No
    REWRITE No No Yes No
    WRITE No Yes No Yes
    UNLOCK Yes Yes Yes Yes

Writing a Sequential File

Each WRITE statement appends a logical record to the end of an output file, thereby creating an entirely new record in the file. The WRITE statement appends records to files that are OPEN for the following modes:

Table 6-4 Valid I/O Statements for Line Sequential Files (Alpha)
      Open Mode
File
Organization
Access
Mode
Statement INPUT OUTPUT EXTEND
LINE
SEQUENTIAL
SEQUENTIAL READ Yes No No
    WRITE No Yes Yes
    UNLOCK Yes Yes Yes

Writing a Line Sequential File (Alpha)

Each WRITE statement appends a logical record to the end of an output file, thereby creating an entirely new record in the file. The WRITE statement appends records to files that are OPEN for the following modes:

Writing a Record

You can write records in the following two ways:

The first way provides easier program readability with multiple record types. For example, statements (1) and (2) in the following example are logically equivalent:


FILE SECTION. 
FD  STOCK-FILE. 
01  STOCK-RECORD       PIC X(80). 
WORKING-STORAGE SECTION. 
01  STOCK-WORK         PIC X(80). 
 
----------------(1)----------------     --------------(2)--------------- 
WRITE STOCK-RECORD FROM STOCK-WORK.     MOVE STOCK-WORK TO STOCK-RECORD. 
                                        WRITE STOCK-RECORD. 

When you omit the FROM phrase, you process the records directly in the record area or buffer (for example, STOCK-RECORD).

The following example writes the record PRINT-LINE to the device assigned to that record's file, then skips three lines. At the end of the page (as specified by the LINAGE clause), it causes program control to transfer to HEADER-ROUTINE.


WRITE PRINT-LINE BEFORE ADVANCING 3 LINES 
      AT END-OF-PAGE PERFORM HEADER-ROUTINE. 

For a WRITE FROM statement, if the destination area is shorter than the file's record length, the destination area is padded on the right with spaces; if longer, the destination area is truncated on the right. This follows the rules for a group move.

6.3.3 File Handling for Relative Files

Creating a relative file involves the following tasks:

  1. Specifying ORGANIZATION IS RELATIVE in the Environment Division SELECT clause
  2. Specifying ACCESS MODE IS SEQUENTIAL (or RANDOM) in the Environment Division SELECT clause
    Each of these two access modes requires a different processing technique. (Refer to the Creating a Relative File in Sequential Access Mode and Creating a Relative File in Random Access Mode sections in this chapter for information about those techniques.)
  3. Opening the file for OUTPUT or I-O
  4. Initializing the relative key data name for each new record
  5. Executing a WRITE statement for each new relative record
  6. Closing the file

Creating a Relative File in Sequential Access Mode

When your program creates a relative file in sequential access mode, the I/O system does not use the relative key. Instead, it writes the first record in the file at relative record number 1, the second record at relative record number 2, and so on, until the program closes the file. If you use the RELATIVE KEY IS clause, the compiler moves the relative record number of the record being written to the relative key data item. Example 6-24 writes 10 records with relative record numbers 1 to 10.

Example 6-24 Creating a Relative File in Sequential Access Mode

IDENTIFICATION DIVISION. 
PROGRAM-ID. REL02. 
ENVIRONMENT DIVISION. 
INPUT-OUTPUT SECTION. 
FILE-CONTROL. 
    SELECT FLAVORS ASSIGN TO "BRAND" 
                   ORGANIZATION IS RELATIVE 
                   ACCESS MODE IS SEQUENTIAL. 
DATA DIVISION. 
FILE SECTION. 
FD  FLAVORS. 
01  KETCHUP-MASTER. 
    02  FILLER            PIC X(14). 
    02  REC-NUM           PIC 9(05). 
    02  FILLER            PIC X(31). 
    02  FILLER            PIC X(31). 
WORKING-STORAGE SECTION. 
01  REC-COUNT             PIC S9(5) VALUE 0. 
PROCEDURE DIVISION. 
A000-BEGIN. 
    OPEN OUTPUT FLAVORS. 
    PERFORM A010-WRITE 10 TIMES. 
    CLOSE FLAVORS. 
    STOP RUN. 
A010-WRITE. 
    MOVE "Record number" TO KETCHUP-MASTER. 
    ADD 1 TO REC-COUNT. 
    MOVE REC-COUNT TO REC-NUM. 
    WRITE KETCHUP-MASTER 
          INVALID KEY DISPLAY "BAD WRITE" 
                      STOP RUN. 

Creating a Relative File in Random Access Mode

When a program creates a relative file using random access mode, the program must place a value in the RELATIVE KEY data item before executing a WRITE statement. Example 6-25 shows how to supply the relative key. It writes 10 records in the cells numbered: 2, 4, 6, 8, 10, 12, 14, 16, 18, and 20. Record cells 1, 3, 5, 7, 9, 11, 13, 15, 17, and 19 are also created, but contain no valid records.

Example 6-25 Creating a Relative File in Random Access Mode

IDENTIFICATION DIVISION. 
PROGRAM-ID. REL03. 
ENVIRONMENT DIVISION. 
INPUT-OUTPUT SECTION. 
FILE-CONTROL. 
    SELECT FLAVORS ASSIGN TO "BRAND" 
                   ORGANIZATION IS RELATIVE 
                   ACCESS MODE IS RANDOM 
                   RELATIVE KEY IS KETCHUP-MASTER-KEY. 
DATA DIVISION. 
FILE SECTION. 
FD  FLAVORS. 
01  KETCHUP-MASTER. 
    02  FILLER            PIC X(14). 
    02  REC-NUM           PIC 9(05). 
    02  FILLER            PIC X(31). 
WORKING-STORAGE SECTION. 
01  KETCHUP-MASTER-KEY    PIC 99. 
01  REC-COUNT             PIC S9(5) VALUE 0. 
PROCEDURE DIVISION. 
A000-BEGIN. 
    OPEN OUTPUT FLAVORS. 
    MOVE 0 TO KETCHUP-MASTER-KEY. 
    PERFORM A010-CREATE-RELATIVE-FILE 10 TIMES. 
    DISPLAY "END OF JOB". 
    CLOSE FLAVORS. 
    STOP RUN. 
A010-CREATE-RELATIVE-FILE. 
    ADD 2 TO KETCHUP-MASTER-KEY. 
    MOVE "Record number" TO KETCHUP-MASTER. 
    ADD 2 TO REC-COUNT. 
    MOVE REC-COUNT TO REC-NUM. 
    WRITE KETCHUP-MASTER 
          INVALID KEY DISPLAY "BAD WRITE" 
                      STOP RUN. 

Statements for Relative File Processing

Processing a relative file involves the following:

  1. Opening the file
  2. Setting the relative record number
  3. Processing the file with valid I/O statements
  4. Closing the file

Table 6-5 lists the valid I/O statements and illustrates the following relationships:

Table 6-5 Valid I/O Statements for Relative Files
      Open Mode
File
Organization
Access
Mode
Statement INPUT OUTPUT I-O EXTEND
RELATIVE SEQUENTIAL DELETE
READ
REWRITE
START
WRITE
UNLOCK
No
Yes
No
Yes
No
Yes
No
No
No
No
Yes
Yes
Yes
Yes
Yes
Yes
No
Yes
No
No
No
No
Yes
Yes
  RANDOM DELETE
READ
REWRITE
WRITE
UNLOCK
No
Yes
No
No
Yes
No
No
No
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
No
No
No
No
  DYNAMIC DELETE
READ
READ NEXT
REWRITE
START
WRITE
UNLOCK
No
Yes
Yes
No
Yes
No
Yes
No
No
No
No
No
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
No
No
No
No
No
No

Writing a Relative File

Each WRITE statement places a record into a cell that contains no valid data. If the cell does not already exist, the I/O system creates it. To change the contents of a cell that already contains valid data, use the REWRITE statement.


Previous Next Contents Index