HP OpenVMS Systems Documentation

Content starts here

HP COBOL
User Manual


Previous Contents Index

Reading an Indexed File from Other Languages on Tru64 UNIX

COBOL supports more data types for indexed keys than are supported in the ISAM definition. For keys in any of the data types not supported in the ISAM definition, the run-time system will translate those keys to strings. Table 6-7 specifies the appropriate mapping to create or use indexed files outside of COBOL (for example, if you are using the C language on Tru64 UNIX and you need to access COBOL files). Refer to the ISAM package documentation for details of the file format.

Table 6-7 Indexed File---ISAM Mapping
COBOL Data Type Maps To Transformation Method
character string
PIC x(n)
CHARTYPE None.
short signed int
PIC S9(4) COMP
INTTYPE C-ISAM
long signed int
PIC S9(9) COMP
LONGTYPE C-ISAM
signed quadword
PIC S9(18) COMP
CHARTYPE Reverse the bytes (integers: most significant byte (msb) last; character strings: msb first).

If the data type is not _UNSIGNED, then complement the sign bit. This causes negative values to sort correctly with respect to each other, and precede positive values.
unsigned quadword
PIC 9(18) COMP
CHARTYPE Same as signed quadword.
packed decimal
PIC S9(n) COMP-3
CHARTYPE (Note that sign nibble after is the only case allowed in COBOL.) If the sign nibble is minus, complement all bits. This will give a sign nibble of 1 for a minus, which will come before the plus.

Copy the nibbles so the sign nibble is placed on the left and all the other nibbles are shifted one to the right.

Note that any data type not directly supported by ISAM is translated to a character string, which will sort as a character string in the correct order. <>

6.5 Updating Files

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

  1. Opening the file
  2. Executing a READ or START statement
  3. Executing a REWRITE and a DELETE statement

Sections 6.5.1, 6.5.2, and 6.5.3 describe how to update sequential, relative, and indexed files.

6.5.1 Updating a Sequential File or Line Sequential (Alpha, I64) File

Updating a record in a sequential file involves the following:

  1. Opening the file for I/O
  2. Reading the target record
  3. Rewriting the target record

The REWRITE statement places the record just read back into the file. The REWRITE statement completely replaces the contents of the target record with new data. You can use the REWRITE statement for files on mass storage devices only (for example, disk units). There are two ways of rewriting records:

  • REWRITE record-name FROM source-area
  • REWRITE record-name

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)--------------
REWRITE STOCK-RECORD FROM STOCK-WORK.   MOVE STOCK-WORK TO STOCK-RECORD.
                                        REWRITE STOCK-RECORD.

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

For a REWRITE statement on a sequential file, the record being rewritten must be the same length as the record being replaced.

Example 6-37 reads a sequential file and rewrites as many records as the operator wants.

Example 6-37 Rewriting a Sequential File

IDENTIFICATION DIVISION.
PROGRAM-ID. SEQ03.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT TRANS-FILE ASSIGN TO "TRANS".
DATA DIVISION.
FILE SECTION.
FD  TRANS-FILE.
01  TRANSACTION-RECORD    PIC X(25).
WORKING-STORAGE SECTION.
01  ANSWER                PIC X.
PROCEDURE DIVISION.
A000-BEGIN.
    OPEN I-O TRANS-FILE.
    PERFORM A100-READ-TRANS-FILE
       UNTIL TRANSACTION-RECORD = "END".
    CLOSE TRANS-FILE.
    STOP RUN.
A100-READ-TRANS-FILE.
    READ TRANS-FILE AT END
       MOVE "END" TO TRANSACTION-RECORD.
    IF TRANSACTION-RECORD NOT = "END"
       PERFORM A300-GET-ANSWER UNTIL ANSWER = "Y" OR "N"
        IF ANSWER = "Y" DISPLAY "Please enter new record content"
           ACCEPT TRANSACTION-RECORD
           REWRITE TRANSACTION-RECORD.
A300-GET-ANSWER.
    DISPLAY "Do you want to replace this record? -- "
             TRANSACTION-RECORD.
    DISPLAY "Please answer Y or N".
    ACCEPT ANSWER.

You cannot open a line sequential file (Alpha, I64) for I-O or use the REWRITE statement. <>

Extending a Sequential File or Line Sequential File (Alpha, I64)

To position a file to its current end, and to allow the program to write new records beyond the last record in the file, use both:

  • The EXTEND phrase of the OPEN statement
  • The WRITE statement

Example 6-38 shows how to extend a sequential file.

Example 6-38 Extending a Sequential File or Line Sequential File (Alpha, I64)

IDENTIFICATION DIVISION.
PROGRAM-ID. SEQ04.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
   SELECT TRANS-FILE ASSIGN TO "TRANS".
DATA DIVISION.
FILE SECTION.
FD  TRANS-FILE.
01  TRANSACTION-RECORD    PIC X(25).
PROCEDURE DIVISION.
A000-BEGIN.
    OPEN EXTEND TRANS-FILE.
    PERFORM A100-WRITE-RECORD
       UNTIL TRANSACTION-RECORD = "END".
    CLOSE TRANS-FILE.
    STOP RUN.
A100-WRITE-RECORD.
    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.

Without the EXTEND mode, an HP COBOL program would have to open the input file, copy it to an output file, and add records to the output file.

6.5.2 Updating a Relative File

A program updates a relative file with the WRITE, REWRITE, and DELETE statements. The WRITE statement adds a record to the file. Only the REWRITE and DELETE statements change the contents of records already existing in the file. In either case, adequate backup must be available in the event of error. Sections 6.5.2.1 and 6.5.2.2 explain how to rewrite and delete relative records, respectively.

6.5.2.1 Rewriting a Relative File

The REWRITE statement logically replaces a record in a relative file; the original contents of the record are lost. Two options are available for rewriting relative records:

  • Sequential access mode rewriting
  • Random access mode rewriting

Rewriting Relative Records in Sequential Access Mode

Rewriting relative records in sequential access mode involves the following:

  1. Specifying ORGANIZATION IS RELATIVE in the Environment Division SELECT clause
  2. Specifying ACCESS MODE IS SEQUENTIAL in the Environment Division SELECT clause
  3. Opening the file for I-O
  4. Using a START statement and then a READ statement to read the target record
  5. Updating the record
  6. Rewriting the record into its cell

Example 6-39 reads a relative record sequentially and displays the record on the terminal. The program then passes the record to an update routine that is not included in the example. The update routine updates the record, and passes the updated record back to the program illustrated in Example 6-39, which displays the updated record on the terminal and rewrites the record in the same cell.

Example 6-39 Rewriting Relative Records in Sequential Access Mode

IDENTIFICATION DIVISION.
PROGRAM-ID. REL07.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT FLAVORS ASSIGN TO "BRAND"
                   ORGANIZATION IS RELATIVE
                   ACCESS MODE IS SEQUENTIAL
                   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 VALUE 99.
PROCEDURE DIVISION.
A000-BEGIN.
    OPEN I-O FLAVORS.
    PERFORM A100-UPDATE-RECORD UNTIL KETCHUP-MASTER-KEY = 00.
A005-EOJ.
    DISPLAY "END OF JOB".
    CLOSE FLAVORS.
    STOP RUN.
A100-UPDATE-RECORD.
    DISPLAY "TO UPDATE A RECORD ENTER ITS RECORD NUMBER (ZERO to END)".
    ACCEPT KETCHUP-MASTER-KEY WITH CONVERSION.
    IF KETCHUP-MASTER-KEY IS NOT EQUAL TO 00
       START FLAVORS KEY IS EQUAL TO KETCHUP-MASTER-KEY
             INVALID KEY DISPLAY "BAD START"
                         STOP RUN.
                    END-START
       PERFORM A200-READ-FLAVORS
       DISPLAY  "*********BEFORE UPDATE*********"
       DISPLAY KETCHUP-MASTER
************************************************************
*
*      Update routine code here
*
************************************************************
       DISPLAY  "*********AFTER UPDATE*********"
       DISPLAY KETCHUP-MASTER
       REWRITE KETCHUP-MASTER.
A200-READ-FLAVORS.
    READ FLAVORS
         AT END DISPLAY "END OF FILE"
                GO TO A005-EOJ.

Rewriting Relative Records in Random Access Mode


Previous Next Contents Index