[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP COBOL
User Manual


Previous Contents Index

Writing a Line Sequential File (Alpha, I64)

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:

  • WRITE record-name FROM source-area
  • WRITE record-name

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:

  • Organization determines valid access modes.
  • Organization and access mode determine valid open modes.
  • All three (organization, access, and open mode) enable or disable I/O statements.

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