HP 3000 Manuals

Logical Files [ HP COBOL II/XL Programmer's Guide ] MPE/iX 5.0 Documentation


HP COBOL II/XL Programmer's Guide

Logical Files 

A logical file is a data structure that your program declares and
accesses.  Your program can declare logical files of these four types:

   *   Sequential organization, including MPE special files.

   *   Random access.

   *   Relative organization.

   *   Indexed organization.

Each file type name reflects the way files of that type are organized and
can be accessed.  Organization and access method, the major attributes of
a file type, determine some of its other attributes.

This section explains the above file types and variable records, which
every file type can have.

Table 5-2  summarizes the attributes of the four file types for the
purpose of comparison.  This section explains each file type in detail.
Definitions and explanations of some of the terms in Table 5-2  follow
the table.

          Table 5-2.  Attributes of File Types 

----------------------------------------------------------------------------------------------------
|                      |                                 File Type                                 |
|                      |                                                                           |
|      Attribute       |                                                                           |
----------------------------------------------------------------------------------------------------
|                      |    Sequential    |      Random      |     Relative     |     Indexed      |
|                      |   Organization   |      Access      |   Organization   |   Organization   |
----------------------------------------------------------------------------------------------------
| Key Type             | Does not use     | Numeric.         | Numeric.         | Alphanumeric.    |
|                      | keys.            |                  |                  | Must be written  |
|                      |                  |                  |                  | in ascending     |
|                      |                  |                  |                  | order if access  |
|                      |                  |                  |                  | mode is          |
|                      |                  |                  |                  | sequential.      |
----------------------------------------------------------------------------------------------------
- Key Quantity         -                  - One              - One              - 1 to 16.         -
----------------------------------------------------------------------------------------------------
- Is Key Unique?       -                  - Yes              - Yes              - No               -
----------------------------------------------------------------------------------------------------
- First Key            -                  - Zero             - One              - Any value.       -
----------------------------------------------------------------------------------------------------
- Open Mode:  Input    - Yes              - Yes              - Yes              - Yes              -
----------------------------------------------------------------------------------------------------
- Open Mode:  Output   - Yes              - Yes              - Yes              - Yes              -
----------------------------------------------------------------------------------------------------
| Open Mode:           | Yes              | Yes              | Yes              | Yes              |
| Input-Output         |                  |                  |                  |                  |
----------------------------------------------------------------------------------------------------
- Open Mode:  Extend   - Yes              - No               - Yes              - Yes              -
----------------------------------------------------------------------------------------------------
- Sequential Access    - Yes              - Yes              - Yes              - Yes              -
----------------------------------------------------------------------------------------------------
- Random Access        - No               - Yes              - Yes              - Yes              -
----------------------------------------------------------------------------------------------------
- Dynamic Access       - No               - No               - Yes              - Yes              -
----------------------------------------------------------------------------------------------------
| Records can be       | Yes              | Yes              | Yes              | Yes              |
| appended             |                  |                  |                  |                  |
----------------------------------------------------------------------------------------------------
| Records can be       | No               | No               | Yes              | Yes              |
| deleted              |                  |                  |                  |                  |
----------------------------------------------------------------------------------------------------
| Records can be       | No               | Yes              | Yes              | Yes              |
| inserted             |                  |                  |                  |                  |
----------------------------------------------------------------------------------------------------
| Records can be       | In place.        | Yes              | Yes              | Yes[REV BEG]     |
| updated              |                  |                  |                  |                  |
----------------------------------------------------------------------------------------------------
| File portability     | Completely       | Portable to MPE. | Portable to MPE. | Portable to      |
|                      | portable.        |                  |                  | MPE.[REV END]    |
----------------------------------------------------------------------------------------------------
| Affects program      | No               | Yes              | No               | No               |
| portability          |                  |                  |                  |                  |
----------------------------------------------------------------------------------------------------
| Space is allocated   | Records written. | Records possible | Records possible | [REV BEG]In      |
| for:                 |                  | (maximum key     | (maximum key     | Compatibility    |
|                      |                  | value plus one,  | value) and one   | Mode, two files: |
|                      |                  | because first    | tag per record.  | one for records  |
|                      |                  | key is zero.)    |                  | written and one  |
|                      |                  |                  |                  | for bookkeeping. |
|                      |                  |                  |                  | In Native Mode,  |
|                      |                  |                  |                  | one file.[REV    |
|                      |                  |                  |                  | END]             |
----------------------------------------------------------------------------------------------------
| Device on which file | Any              | Disk only.       | Disk only.       | Disk only.       |
| can reside:          |                  |                  |                  |                  |
----------------------------------------------------------------------------------------------------

Below are definitions of the terms in column one of Table 5-2 .

Term                      Definition                                                                                                 

Key                       A value within a record that serves to
                          distinguish it from other records. 

Input open mode           Allows a program to read a file, but not write
                          it.  A file that is open for input access can
                          be used for input, but not output. 

Output open mode          Allows a program to write a file, but not read
                          it.  A file that is open for output access can
                          be used for output, but not input. 

Input-Output open mode    Allows a file to be read and written.  A file
                          that is open for input-output access can be
                          used for input and/or output. 

Extend open mode          Allows a file to be written in sequential
                          access mode only. 

Sequential organization   Allows file records to be read in order from
                          first to last, appended to a file, or written
                          one after another. 

Random access             Allows file records to be read or written in
                          any order. 

Dynamic access            Allows file to be accessed sequentially or
                          randomly. 

Append                    To append a record to a file is to add a new
                          record to the end of the file. 

Delete                    To delete a record from a file is to remove the
                          record from the file. 

Insert                    To insert a record into a file is to add a new
                          record to the file between two of its existing
                          records (if there is room in the key order). 

Update                    To update a record is to change its content
                          (without changing its position in the file). 

File portability          The degree to which a file is portable; the
                          number of computers on which it can be used,
                          other than the one on which it was created.  A
                          completely portable file can be used on any
                          computer.  A file that is portable to MPE can
                          be used on any MPE computer (if all its data is
                          USAGE DISPLAY). 

Program portability       The degree to which a program is portable; the
                          number of computers on which it can be compiled
                          and/or run, other than the one on which it was
                          originally compiled.  File type can affect
                          program portability.  A file type that requires
                          nonportable procedures to access it makes
                          programs that use files of that type
                          nonportable.

Sequential Organization Files 

A sequential organization file is so named because it can only be
accessed sequentially.  It does not use keys.  Because of this
simplicity, a sequential organization file:

   *   Is completely portable (when you are making an ANSI LABELLED TAPE
       and all data in the records is USAGE DISPLAY).

   *   Does not limit the portability of the programs that use it.

   *   Requires space only for records that are actually written to it.

   *   Can reside on any device.

This section explains the following:

   *   How to code sequential organization files.

   *   MPE special files (specialized sequential organization files):

          *   Circular files.

          *   Message files.

          *   Print files.

Table 5-3  lists the access modes for sequential organization files,
the open modes associated with them, and the I-O statements that are
valid with those open modes.

          Table 5-3.  Access Modes, Open Modes, and Valid I-O Statements for 
                      Sequential Organization Files 
-------------------------------------------------------------------------------------------------
|              |              |                   |                                             |
| Access Mode  |  Open Mode   | Valid Statements  |                 Explanation                 |
|              |              |                   |                                             |
-------------------------------------------------------------------------------------------------
|              |              |                   |                                             |
| Read-only    | INPUT        | READ              | You can read the file from beginning to     |
|              |              |                   | end.                                        |
|              |              |                   |                                             |
-------------------------------------------------------------------------------------------------
|              |              |                   |                                             |
| Write-only   | OUTPUT       | WRITE             | You can write the file from beginning to    |
|              |              |                   | end.  If the file exists, it is             |
|              |              |                   | overwritten.  If it does not exist, it is   |
|              |              |                   | created.                                    |
|              |              |                   |                                             |
-------------------------------------------------------------------------------------------------
|              |              |                   |                                             |
|              | EXTEND       | WRITE             | You can append records to the file.  If the |
|              |              |                   | file does not exist, it is created if the   |
|              |              |                   | SELECT statement specifies the OPTIONAL     |
|              |              |                   | phrase.                                     |
|              |              |                   |                                             |
-------------------------------------------------------------------------------------------------
|              |              |                   |                                             |
| Read-write   | I-O          | READ, REWRITE;    | You can process the file from beginning to  |
|              |              | WRITE if the      | end.  You can REWRITE a record immediately  |
|              |              | physical file is  | after a READ (update it in place).  You     |
|              |              | a terminal (HP    | cannot add new records.                     |
|              |              | extension)        |                                             |
|              |              |                   |                                             |
-------------------------------------------------------------------------------------------------

A sequential organization file is appropriate for a program that reads or
writes a file from beginning to end, without skipping around in it.
Examples are transaction files (which are read from beginning to end) and
back-up files (which are written from beginning to end).

How to Code Sequential Organization Files.   

The minimum code your program needs to perform input and output with
sequential organization files is:

   *   In the ENVIRONMENT DIVISION, a SELECT statement with an ASSIGN
       clause for each file.

   *   In the FILE SECTION of the DATA DIVISION, an FD entry to match
       each SELECT statement, with an 01 record for each file.

   *   In the PROCEDURE DIVISION, procedures to OPEN, READ, WRITE, and
       CLOSE the files.

Example 1.   

The following uses a sequential organization file:

     IDENTIFICATION DIVISION.
     PROGRAM-ID. FILE-EX1.
     ENVIRONMENT DIVISION.
     INPUT-OUTPUT SECTION.
     FILE-CONTROL.
         SELECT IFILE             ASSIGN "IFILE".
         SELECT PFILE             ASSIGN "PFILE".
     DATA DIVISION.
     FILE SECTION.
     FD  IFILE.
     01  IREC.
         05  NAME                 PIC X(30).
         05  SOC-SEC              PIC X(9).
         05  HIRE-DATE.
             10 MO                PIC XX.
             10 DA                PIC XX.
             10 YR                PIC XX.
         05  SALARY               PIC S9(6).
         05                       PIC X(29).
     FD  PFILE.
     01  PREC.
         05  SOC-SEC              PIC X(9).
         05                       PIC XX.
         05  NAME                 PIC X(30).
         05                       PIC XX.
         05  HIRE-DATE.
             10  MO               PIC XX.
             10                   PIC X.
             10  DA               PIC XX.
             10                   PIC X.
             10  YR               PIC XX.
         05                       PIC X(81).
     01  HREC.
         05  HSOC-SEC             PIC X(11).
         05  HNAME                PIC X(32).
         05  HHIRE-DATE           PIC X(89).
     WORKING-STORAGE SECTION.
     01  LNCNT                    PIC S9(4) BINARY VALUE 60.
     01  W-DATE.
         05  WYR                  PIC XX.
         05                       PIC X(4).
     PROCEDURE DIVISION.
     P1.
         ACCEPT W-DATE FROM DATE.
         OPEN INPUT IFILE OUTPUT PFILE.
         PERFORM WITH TEST AFTER UNTIL SOC-SEC OF IREC = ALL "9"
             READ IFILE
                 AT END MOVE ALL "9" TO SOC-SEC OF IREC
                 NOT AT END
                     IF WYR = YR OF IREC THEN
                         ADD 1 TO LNCNT
                         IF LNCNT > 50 PERFORM HEADINGS END-IF
                         MOVE SPACES TO PREC
                         MOVE CORR IREC TO PREC
                         WRITE PREC AFTER ADVANCING 1 LINE
                     END-IF
             END-READ
         END-PERFORM
         CLOSE IFILE PFILE
         STOP RUN.
     HEADINGS.
         MOVE "SOC SEC NO" TO HSOC-SEC.
         MOVE "NAME" TO HNAME.
         MOVE "HIRE DATE" TO HHIRE-DATE.
         WRITE PREC AFTER ADVANCING PAGE.
         MOVE 0 TO LNCNT.

The following is input to the program:

        Albert Einstein               343567890010587
        James Joyce                   123456789033086
        Alice Walker                  987654321020187
        Rolando Jiron                 333444555121085

This program prints the following:

        SOC SEC NO NAME                           HIRE DATE
        343567890  ALBERT EINSTEIN                01 05 87
        987654321  ALICE WALKER                   02 01 87

The FILE STATUS Clause.   

The optional FILE STATUS clause specifies a data-item that contains a
file status code after any I-O verb (READ, WRITE, OPEN, or CLOSE) is
applied to the file.  Your program can also contain USE procedures that
examine the values of such data-items and perform accordingly.  See "File
Status Codes" for more information.

The BLOCK CONTAINS Clause.   

The BLOCK CONTAINS clause is not required.  It is better to set the block
size outside of the program, when you create the file with the FILE or
BUILD command.

The RESERVE Clause.   

The RESERVE clause specifies the number of file system buffers assigned
to a COBOL program at execution time.  The default is two buffers, which
is optimal for most COBOL programs.  A program with extremely heavy I-O
and a set of frequently accessed records may perform better with three
buffers.  Allocating more than three buffers is inefficient use of memory
and rarely improves I-O performance.

The CODE-SET Clause.   

By default, a sequential file contains ASCII data.  If your sequential
file contains non-ASCII data, you must use the CODE-SET clause to specify
its character code convention.

Example.   

The following program illustrates the CODE-SET clause:

      IDENTIFICATION DIVISION.
      PROGRAM-ID. FILE-EX1.
     * This program converts an EBCDIC file into an ASCII file.

      ENVIRONMENT DIVISION.
      CONFIGURATION SECTION.
      SPECIAL-NAMES.
          ALPHABET EBCDIC IS EBCDIC.

      INPUT-OUTPUT SECTION.
      FILE-CONTROL.
          SELECT IFILE             ASSIGN "IFILE".
          SELECT OFILE             ASSIGN "OFILE".
      DATA DIVISION.
      FILE SECTION.
      FD  IFILE
          CODE-SET IS EBCDIC.
      01  IREC                     PIC X(80).
      FD  OFILE.
      01  OREC                     PIC X(80).
      PROCEDURE DIVISION.
      P1.
          OPEN INPUT IFILE OUTPUT OFILE.
          PERFORM WITH TEST AFTER  UNTIL IREC = ALL "9"
              READ IFILE
                  AT END MOVE ALL "9" TO IREC
                  NOT AT END
                      MOVE IREC TO OREC
                      WRITE OREC
              END-READ
          END-PERFORM
          CLOSE IFILE OFILE.


NOTE A non-ASCII file need not be organized sequentially for a COBOL program to read or write it.
In the preceding examples, the records of each file are of the same format. A file with variable records requires a RECORD clause. See "Variable Records." Circular Files. A circular file is organized like a sequential organization file, except that it has no "last" record when being written. The record that would be "last" in an ordinary sequential organization file is followed (conceptually) by the record that would be "first." The diagrams below shows an ordinary sequential organization file with eight records and a circular file with eight records, for comparison. The following shows an ordinary sequential organization file with eight records. ----------------------------------------------------------------------------------------------------------------- - Record 1 - Record 2 - Record 3 - Record 4 - Record 5 - Record 6 - Record 7 - Record 8 - ----------------------------------------------------------------------------------------------------------------- The following shows a circular file with eight records (also a sequential organization file): ---------------------------------------------------------------------------------------------- - Record 1 - Record 2 - Record 3 - ---------------------------------------------------------------------------------------------- - Record 8 - - Record 4 - ---------------------------------------------------------------------------------------------- - Record 7 - Record 6 - Record 5 - ---------------------------------------------------------------------------------------------- The two ways to create a circular file are: * Use the MPE BUILD command, like this: BUILD filename;CIR * Use the MPE FILE command to cause COBOL to create a circular file, like this: FILE filename;CIR A circular file is appropriate for a history file. A circular file with n records keeps track of the last n transactions, and never fills up. Example. This example program uses the following: * A circular file for output. * A variable record file input. See "Variable Records." * A SYMBOLIC CHARACTERS clause, an ANSI85 feature. See Chapter 2 . Assuming that the program is in the file named filex4, you can use the following sequence of MPE XL commands to run it: :file ifile=$stdin :file ofile,new;cir;rec=-80,,,ascii;disc=20 :cob85xlg filex4 These commands tell the program to do the following: * Read input from the terminal, unless the program is run from a job stream. * Write output to a circular file that can hold 20 records.
NOTE The program does not require a circular output file.
IDENTIFICATION DIVISION. PROGRAM-ID. FILE-EX4. * Reads input from terminal to variable record file. Writes to circular * file. Input consists of commands. Last commands entered can be found * in circular file. Number of records logged depends on file size. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SPECIAL-NAMES. SYMBOLIC CHARACTERS CR IS 14. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT IFILE ASSIGN "IFILE". SELECT OFILE ASSIGN "OFILE". DATA DIVISION. FILE SECTION. FD IFILE RECORD IS VARYING DEPENDING ON LEN. 01 IREC. 05 ICHARS PIC X OCCURS 0 TO 80 TIMES DEPENDING ON LEN. FD OFILE. 01 OREC PIC X(80). WORKING-STORAGE SECTION. 01 LEN PIC S9(4) BINARY. 01 ERROR-CODE PIC S9(4) BINARY. 01 PARM PIC S9(4) BINARY. PROCEDURE DIVISION. P1. OPEN INPUT IFILE OUTPUT OFILE. PERFORM WITH TEST AFTER UNTIL IREC = "//" READ IFILE AT END MOVE 2 TO LEN MOVE "//" TO IREC NOT AT END IF IREC "//" ADD 1 TO LEN MOVE CR TO ICHARS(LEN) CALL INTRINSIC "COMMAND" USING IREC ERROR-CODE PARM WRITE OREC FROM IREC END-IF END-READ END-PERFORM CLOSE IFILE OFILE STOP RUN. Message Files. A message file is organized like a sequential organization file that is open for input or output access. Programs use message files to communicate with each other. The two ways to create a message file are: * Use the MPE BUILD command, like this: BUILD filename;MSG * Use the MPE FILE command to cause COBOL to create a message file, like this: FILE filename;MSG You must open a message file with the INPUT, OUTPUT, or EXTEND option. Sometimes you must call the FCONTROL intrinsic for full functionality, for example: * To set a timeout interval. * To enable an extended wait. An extended wait allows your program to wait until another program has read or written the message file before it accesses it. For example, your program can wait until another program has written a message to the message file before reading it, or your program can wait until another program has read the message file before writing it. Example. Suppose that you have a summary job that cannot run until five other jobs have run. The five other jobs are not interdependent; they can run at the same time. The problem is how to ensure that they have run before the summary job runs. One solution is to use a message file. If each of the five jobs logs information into the message file, the summary job can wait until all five jobs have accessed the message file before it executes. The program for the summary job follows. Notice that it uses FCONTROL to enable extended wait. It calls FCONTROL directly, passing the FD name (IFILE) to the intrinsic as the file number. (Any COBOL program can call any MPE intrinsic in this manner. The access mode of the file is not important.) 001000 IDENTIFICATION DIVISION. 002000 PROGRAM-ID. FILEX6. 003000 ENVIRONMENT DIVISION. 004000 INPUT-OUTPUT SECTION. 005000 FILE-CONTROL. 006000 SELECT IFILE ASSIGN "MFILE". 007000 DATA DIVISION. 008000 FILE SECTION. 009000 FD IFILE. 010000 01 IREC PIC X(8). 011000 WORKING-STORAGE SECTION. 012000 01 TRUE-VALUE PIC 9(4) BINARY VALUE 1. 013000 01 PROGRAM-COUNT PIC S9(4) BINARY VALUE 0. 014000 01 DEPENDENCY-TABLE. 015000 05 PIC X(8) VALUE "FILEX51". 016000 05 PIC X(8) VALUE "FILEX52". 017000 05 PIC X(8) VALUE "FILEX53". 018000 05 PIC X(8) VALUE "FILEX54". 019000 05 PIC X(8) VALUE "FILEX55". 020000 01 REDEFINES DEPENDENCY-TABLE. 021000 05 PROGRAM-NAME PIC X(8) OCCURS 5 TIMES 022000 ASCENDING KEY IS PROGRAM-NAME 023000 INDEXED BY I. 024000 PROCEDURE DIVISION. 025000 P1. 026000 WAITING-TO-GO. 027000 OPEN INPUT IFILE. 028000 CALL INTRINSIC "FCONTROL" USING IFILE, 45, TRUE-VALUE. 029000 PERFORM UNTIL PROGRAM-COUNT = 5 030000 READ IFILE 031000 AT END 032000 DISPLAY "AT END on message file should not occur" 033000 GO TO END-OF-PROGRAM 034000 END-READ 035000 036000 SEARCH ALL PROGRAM-NAME 037000 AT END 038000 DISPLAY "filex6: dependency table needs update" 039000 GO TO END-OF-PROGRAM 040000 WHEN PROGRAM-NAME(I) = IREC 041000 ADD 1 TO PROGRAM-COUNT 042000 END-SEARCH 043000 044000 END-PERFORM 045000 CLOSE IFILE. 046000 047000 048000 MAIN-PROGRAM. 049000 DISPLAY "JOBS FILEX51 THRU FILEX55 COMPLETED," 050000 " FILEX6 CONTINUING". 051000 END-OF-PROGRAM. 052000 STOP RUN. 053000 Each of the five jobs contains code similar to this: 001000 IDENTIFICATION DIVISION. 002000 PROGRAM-ID. FILEX51. 003000 ENVIRONMENT DIVISION. 004000 INPUT-OUTPUT SECTION. 005000 FILE-CONTROL. 006000 SELECT OFILE ASSIGN "MFILE". 007000 DATA DIVISION. 008000 FILE SECTION. 009000 FD OFILE. 010000 01 OREC PIC X(8). 011000 PROCEDURE DIVISION. 012000 P1. 013000 OPEN EXTEND OFILE. 014000 MOVE "FILEX51" TO OREC 015000 WRITE OREC 016000 CLOSE OFILE 017000 STOP RUN. This job stream streams the five jobs and the summary job: :job jfilemsg,user.account :purge mfile :build mfile;msg;rec=-8,,,ascii;disc=10 :stream ,% %job jfilex6,me.myacct/paswd;inpri=7;outclass=pp,3 %file mfile;semi %run pfilex6 %eoj %job jfilex51,me.myacct/paswd;inpri=7;outclass=pp,3 %file mfile;semi %run pfilex51 %eoj %job jfilex52,me.myacct/paswd;inpri=7;outclass=pp,3 %file mfile;semi %run pfilex52 %eoj %job jfilex53,me.myacct/paswd;inpri=7;outclass=pp,3 %file mfile;semi %run pfilex53 %eoj %job jfilex54,me.myacct/paswd;inpri=7;outclass=pp,3 %file mfile;semi %run pfilex54 %eoj %job jfilex55,me.myacct/paswd;inpri=7;outclass=pp,3 %file mfile;semi %run pfilex55 %eoj :eoj Print Files. A print file is organized like a sequential organization file and has carriage control. The carriage control option cannot be changed after the file is created.[REV BEG] See the "WRITE Statement" in the HP COBOL II/XL Reference Manual for more information.[REV END]


MPE/iX 5.0 Documentation