HP OpenVMS Systems Documentation

Content starts here

HP COBOL
User Manual


Previous Contents Index

6.2.2 Specifying File Organization and Record Access Mode

Your program must state---either explicitly or implicitly---a file's organization and record access mode before the program opens the file. The Environment Division ORGANIZATION and ACCESS MODE clauses, if present, specify these two characteristics.

In an HP COBOL program, each file is given a file name in a separate Environment Division SELECT statement. The compiler determines the file organization from the SELECT statement and its associated clauses.

For relative and indexed files, you must specify the ORGANIZATION IS RELATIVE or the ORGANIZATION IS INDEXED phrase, respectively. For sequential files you need not specify the ORGANIZATION IS SEQUENTIAL phrase. For line sequential files (Alpha, I64), you must explicitly declare ORGANIZATION IS LINE SEQUENTIAL. When you omit the ORGANIZATION IS clause the file organization is sequential.

The ASSIGN clause, in the SELECT statement, associates the file name with a file specification. The file specification points the operating system to the file's physical and logical location on a specific hardware device.

The SELECT statement and the ASSIGN clause are further described in Section 6.2.1. For further information, refer to the HP COBOL Reference Manual.

Each file is further described with a file description (FD) entry in the Data Division File Section. The FD entry is followed immediately by the file's record description.

You can specify additional file characteristics in the Environment and Data Divisions as follows:

  • Use the Environment Division APPLY clause to specify file characteristics such as lock-holding, file extension factors, and preallocation factors. (See Chapter 15.)
  • Use file description entries to specify record format and record blocking.
  • Use record description entries to specify physical record size or sizes.

Examples 6-13, 6-14, and Example 6-15 illustrate how to specify the file organization and access mode for sequential, relative, and indexed files.

Example 6-13 Specifying Sequential File Organization and Sequential Access Mode for a Sequential File

IDENTIFICATION DIVISION.
PROGRAM-ID.  SEQ01.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT   MASTER-FILE   ASSIGN   TO   "MASTER.DAT".
    SELECT   TRANS-FILE    ASSIGN   TO   "TRANS.DAT".
    SELECT   REPRT-FILE    ASSIGN   TO   "REPORT.DAT".
DATA DIVISION.
FILE SECTION.
FD  MASTER-FILE.
01  MASTER-RECORD.
    02  MASTER-DATA       PIC X(80).
    02  MASTER-SIZE       PIC 99.
    02  MASTER-TABLE      OCCURS 0 to 50 TIMES
                          DEPENDING ON MASTER-SIZE.
        03  MASTER-YEAR   PIC 99.
        03  MASTER-COUNT  PIC S9(5)V99.
FD  TRANS-FILE.
01  TRANSACTION-RECORD    PIC X(25).
FD  REPRT-FILE.
01  REPORT-LINE           PIC X(132).

Example 6-14 Specifying Relative File Organization and Random Access Mode for a Relative File

IDENTIFICATION DIVISION.
PROGRAM-ID. REL01.
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            PIC X(50).
WORKING-STORAGE SECTION.
01  KETCHUP-MASTER-KEY        PIC 99.

Example 6-15 defines a dynamic access mode indexed file with one primary key and two alternate record keys. Note that one alternate record key allows duplicates. Any program using the identical entries in the SELECT clause as shown in Example 6-15 can reference the DAIRY file sequentially and randomly. Refer to the HP COBOL Reference Manual for information relating to the RECORD KEY and ALTERNATE RECORD KEY clauses.

Example 6-15 Specifying Indexed File Organization and Dynamic Access Mode for an Indexed File

IDENTIFICATION DIVISION.
PROGRAM-ID. INDEX01.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT FLAVORS ASSIGN TO "DAIRY"
             ORGANIZATION IS INDEXED
             ACCESS MODE IS DYNAMIC
             RECORD KEY IS ICE-CREAM-MASTER-KEY
             ALTERNATE RECORD KEY IS ICE-CREAM-STORE-STATE
                              WITH DUPLICATES
             ALTERNATE RECORD KEY IS ICE-CREAM-STORE-CODE.
DATA DIVISION.
FILE SECTION.
FD  FLAVORS.
01  ICE-CREAM-MASTER.
    02 ICE-CREAM-MASTER-KEY          PIC XXXX.
    02 ICE-CREAM-MASTER-DATA.
       03  ICE-CREAM-STORE-CODE      PIC XXXXX.
       03  ICE-CREAM-STORE-ADDRESS   PIC X(20).
       03  ICE-CREAM-STORE-CITY      PIC X(20).
       03  ICE-CREAM-STORE-STATE     PIC XX.
PROCEDURE DIVISION.
A00-BEGIN.
   .
   .
   .

Example 6-16 defines a line sequential (Alpha, I64) file.

Example 6-16 Specifying Line Sequential File Organization with Sequential Access Mode (Alpha, I64)

IDENTIFICATION DIVISION.
PROGRAM ID. EX0616.
ENVIRONMENT DIVISION.
INOUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT MUSIC ASSIGN TO "CLASSICAL"
           ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD  MUSIC.
01  OPERA        PIC X(9).
PROCEDURE DIVISION.
A00-BEGIN.        <>
   .
   .
   .

File organization is discussed in more detail in Section 6.1.1. Record access mode is discussed in the following section.

Record Access Mode

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

  • ACCESS MODE IS SEQUENTIAL
    • With sequential files, sequential access mode retrieves the records in the same sequence established by the WRITE statements that created the file.
    • With relative files, sequential access mode retrieves the records in the order of ascending record key values (or relative record numbers).
    • With indexed files, sequential access mode retrieves records in the order of record key values.
  • ACCESS MODE IS RANDOM---The value of the record key your program specifies indicates the record to be accessed in Indexed and Relative files.
  • ACCESS MODE IS DYNAMIC---With relative and indexed files, dynamic access mode allows you to switch back and forth between sequential access mode and random access mode while reading a file by using the the NEXT phrase on the READ statement. For more information about dynamic access mode, refer to READ and REWRITE statements in the HP COBOL Reference Manual.

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.


Previous Next Contents Index