HP OpenVMS Systems Documentation

Content starts here

HP COBOL
User Manual


Previous Contents Index

6.1.2 Record Format

HP COBOL provides four record format types: fixed, variable, print-control, and stream. Table 6-2 shows the record format availability.

Table 6-2 Record Format Availability
  Sequential Line
Sequential

Relative

Indexed
  Disk Tape      
Fixed length yes yes no yes yes
Variable length yes yes no yes yes
Print control yes no no no no
Stream no no yes no no

The compiler determines the record format from the information that you specify as follows:

  • Fixed record format---Use the RECORD CONTAINS clause. This is the HP COBOL default.
  • Variable record format---Use the RECORD CONTAINS TO clause or the RECORD VARYING clause.
  • Print-control (VFC on OpenVMS systems or ASCII on Tru64 UNIX systems)---use the Procedure Division ADVANCING phrase, the Environment Division APPLY PRINT-CONTROL or (on Tru64 UNIX) ASSIGN TO PRINTER clauses, or the Data Division LINAGE clause, or use Report Writer statements and phrases.
  • Stream (Alpha, I64 only)---Use the FILE-CONTROL ORGANIZATION IS LINE SEQUENTIAL clause. On OpenVMS Alpha and OpenVMS I64 you also get this format with /NOVFC. <>

If a file has more than one record description, the different record descriptions automatically share the same record area in memory. The I/O system does not clear this area before it executes the READ statement. Therefore, if the record read by the latest READ statement does not fill the entire record area, the area not overlaid by the incoming record remains unchanged.

The record format type that was specified when the file was created must be used for all subsequent accesses to the file.

In Example 6-1, a file contains a company's stock inventory information (part number, supplier, quantity, price). Within this file, the information is divided into records. All information for a single piece of stock constitutes a single record.

Example 6-1 Sample Record Description

01  PART-RECORD.
    02  PART-NUMBER                PIC 9999.
    02  PART-SUPPLIER              PIC X(20).
    02  PART-QUANTITY              PIC 99999.
    02  PART-PRICE                 PIC S9(5)V99.

Each record in the stock file is itself divided into discrete pieces of information referred to as elementary items (02 level items). You give each elementary item a specific location in the record, give it a name, and define its size and type. The part number is an elementary item in the part record, as are supplier, quantity, and price. In this example, PART-RECORD contains four elementary items: PART-NUMBER, PART-SUPPLIER, PART-QUANTITY, and PART-PRICE.

Fixed-Length Records

Files with a fixed-length record format contain the same size records. The compiler generates the fixed-length format when either of the following conditions is true:

  • The RECORD CONTAINS clause specifies a fixed number of characters.
  • The RECORD CONTAINS clause is omitted.

The compiler does not generate fixed-length format when any of the following conditions exist:

  • The file description contains a RECORD CONTAINS TO clause or a RECORD VARYING clause.
  • The program specifies a print-control file by referring to the file with:
    • The ADVANCING phrase in a WRITE statement
    • An APPLY PRINT-CONTROL clause in the Environment Division
    • A LINAGE clause in the file description
    • Report Writer statements and phrases
    • ASSIGN TO PRINTER
  • LINE SEQUENTIAL organization is specified.

Fixed-length record size is determined by either the largest record description or the record size specified by the RECORD CONTAINS clause, whichever is larger. Example 6-2 shows how fixed-length record size is determined.

Example 6-2 Determining Fixed-Length Record Size

FD  FIXED-FILE
    RECORD CONTAINS 100 CHARACTERS.
01  FIXED-REC     PIC X(75).

For the file, FIXED-FILE, the RECORD CONTAINS clause specifies a record size larger than the record description; therefore, the record size is 100 characters.

In Example 6-2, the following warning message is generated when the file FIXED-FILE is used:


"Record contains value is greater than length of longest record."

If the multiple record descriptions are associated with the file, the size of the largest record description is used as the size. In Example 6-3, for the file REC-FILE, the FIXED-REC2 record specifies the largest record size; therefore, the record size is 90 characters.

Example 6-3 Determining Fixed-Length Record Size for Files with Multiple Record Descriptions

FD  REC-FILE
    RECORD CONTAINS 80 CHARACTERS.
01  FIXED-REC1    PIC X(75).
01  FIXED-REC2    PIC X(90).

When the file REC-FILE is used, the following warning message is generated:


"Longest record is longer than RECORD CONTAINS value -
   longest record size used."

Variable-Length Records

Files with a variable-length record format can contain records of different length. The compiler generates the variable-length attribute for a file when the file description contains a RECORD VARYING clause or a RECORD CONTAINS TO clause.

Each record is written to the file with a 32-bit integer that specifies the size of the record. This integer is not counted in the size of the record.

Examples 6-4, 6-5, and 6-6 show you the three ways you can create a variable-length record file.

In Example 6-4, the DEPENDING ON phrase sets the OUT-REC record length. The IN-TYPE data field determines the OUT-LENGTH field's contents.

Example 6-4 Creating Variable-Length Records with the DEPENDING ON Phrase

FILE SECTION.
FD  INFILE.
01  IN-REC.
    03  IN-TYPE       PIC X.
    03  REST-OF-REC   PIC X(499).
FD  OUTFILE
    RECORD VARYING FROM 200 TO 500 CHARACTERS
    DEPENDING ON OUT-LENGTH.
01  OUT-REC           PIC X(500).
WORKING-STORAGE SECTION.
01  OUT-LENGTH        PIC 999 COMP VALUE ZEROES.

Example 6-5 shows how to create variable-length records using the RECORD VARYING phrase.

Example 6-5 Creating Variable-Length Records with the RECORD VARYING Phrase

FILE SECTION.
FD  OUTFILE
    RECORD VARYING FROM 200 TO 500 CHARACTERS.
01  OUT-REC-1         PIC X(200).
01  OUT-REC-2         PIC X(500).

Example 6-6 creates variable-length records by using the OCCURS clause with the DEPENDING ON phrase in the record description. HP COBOL determines record length by adding the sum of the variable record's fixed portion to the size of the table described by the number of table occurrences at execution time.

In this example, the variable record's fixed portion size is 113 characters. (This is the sum of P-PART-NUM, P-PART-INFO, and P-BIN-INDEX.) If P-BIN-INDEX contains a 7 at execution time, P-BIN-NUMBER will be 35 characters long. Therefore, PARTS-REC's length will be 148 characters; the fixed portion's length is 113 characters, and the table entry's length at execution time is 35 characters.

Example 6-6 Creating Variable-Length Records and Using the OCCURS Clause with the DEPENDING ON Phrase

     .
     .
     .
FILE SECTION.
FD  PARTS-MASTER
    RECORD VARYING 118 TO 163 CHARACTERS.
01  PARTS-REC.
    03  P-PART-NUM     PIC X(10).
    03  P-PART-INFO    PIC X(100).
    03  P-BIN-INDEX    PIC 999.
    03  P-BIN-NUMBER   PIC X(5)
        OCCURS 1 TO 10 TIMES DEPENDING ON P-BIN-INDEX.
     .
     .
     .

If you describe a record with both the RECORD VARYING...DEPENDING ON phrase on data-name-1 and the OCCURS clause with the DEPENDING ON phrase on data-name-2, HP COBOL specifies record length as the value of data-name-1.


Previous Next Contents Index