Logical Files (continued) [ HP COBOL II/XL Programmer's Guide ] MPE/iX 5.0 Documentation
HP COBOL II/XL Programmer's Guide
Logical Files (continued)
Variable Length Records
[REV BEG]
Variable length records are allowed in every logical file organization.
For random access files and relative organization files, HP COBOL II
simulates variable length records by using fixed length records. HP
COBOL II builds the file with a record size two bytes longer than the
largest logical record, rounded up to a two-byte boundary, defined by the
program. No space is saved. The file must be created by an HP COBOL II
program with the same file characteristics as the program that will
access the file. When creating a random access file with variable length
records, use a file equation like the following to force the creation of
fixed length records:
:FILE X;REC=,,F
Variable length record files created outside of HP COBOL II can be
accessed with ORGANIZATION SEQUENTIAL.
HP COBOL II directly supports variable length records in indexed and
sequential organization files.[REV END]
Specify variable length records with the RECORD IS VARYING clause of the
FD level indicator. The following lists methods of specifying variable
length records that are not recommended:
Method Not Recommended Reason
RECORDING MODE IS V The RECORDING MODE clause is not
ANSI standard.
RECORD CONTAINS integer-4 TO The RECORD CONTAINS clause does not
integer-5 CHARACTERS necessarily create a variable
record file. It only causes the
compiler to check that the record
size is between the values
integer-4 and integer-5.
When reading variable length records, do one of the following:
* In the FILE SECTION, define the record as OCCURS DEPENDING ON,
using the same data item as you use in the DEPENDING ON phrase of
the RECORD IS VARYING and OCCURS clauses.
* Use a READ...INTO statement to blank out the unfilled part of the
record in the WORKING-STORAGE SECTION.
* Before each READ statement executes, blank out the record
associated with the FD level indicator.
Any one of the above ensures that when you overwrite a larger record
value with a smaller one, the record value does not retain "extra"
information from the larger record.
Variable length records are appropriate for any of the following:
* Saving file space on disk. In memory, the maximum space is
allocated, but on disk, the actual size is allocated.
* Reading tapes of unknown format or MPE records of
undefined-length. If this is the case, use the following file
equation:
:FILE logical_filename;DEV=TAPE;REC=,,U
* Terminal I-O, because terminal records are always of undefined
length.[REV BEG] This enables you to know[REV END] how many
characters were entered from the terminal.
Every time you read from a variable length[REV BEG] or
undefined-length file,[REV END] the READ statement assigns the
number of characters read to the data name in the DEPENDING ON
phrase of the RECORD IS VARYING clause.
The terminal is the physical file that is associated with the
logical file. One way to make this association is with the
following file equation:
:FILE logical_file_name;DEV=TERM
* The number of characters written is the exact number of characters
specified by the data name in the DEPENDING ON phrase.
An alternative to a variable length record (RECORD IS VARYING)
that also ensures that the exact number of characters is written
is to define the record in the FILE SECTION with an OCCURS
DEPENDING ON clause.
Example.
The following program illustrates variable length records. It opens the
same[REV BEG] file with and without the DEPENDING ON phrase. Note that
in EXAMPLE 2, IREC2 is not overwritten by a READ statement, but READ INTO
does overwrite the working storage record.[REV END]
IDENTIFICATION DIVISION.
PROGRAM-ID. COBVAR.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IFILE ASSIGN TO "IFILE".
SELECT IFILE2 ASSIGN TO "IFILE".
DATA DIVISION.
FILE SECTION.
FD IFILE
RECORD IS VARYING FROM 10 TO 50 DEPENDING ON LEN.
01 IREC.
05 FILLER PIC X OCCURS 10 TO 50 TIMES DEPENDING ON LEN.
FD IFILE2
RECORD IS VARYING FROM 10 TO 50.
01 IREC2 PIC X(50).
WORKING-STORAGE SECTION.
01 LEN PIC S9(4) BINARY.
01 WREC PIC X(50).
PROCEDURE DIVISION.
P1.
DISPLAY "EXAMPLE 1 OCCURS DEPENDING ON REC"
OPEN INPUT IFILE
PERFORM UNTIL LEN = -1
READ IFILE
AT END MOVE -1 TO LEN
NOT AT END
DISPLAY IREC
DISPLAY LEN
END-READ
END-PERFORM
CLOSE IFILE
DISPLAY SPACE
DISPLAY "EXAMPLE 2 FIXED REC"
OPEN INPUT IFILE2
MOVE ALL "X" TO IREC2
READ IFILE2 AT END MOVE -1 TO LEN
DISPLAY IREC2
DISPLAY SPACE
DISPLAY "EXAMPLE 3 READ INTO WREC"
MOVE ALL "X" TO IREC2 WREC
READ IFILE2 INTO WREC AT END MOVE -1 TO LEN
DISPLAY IREC2
DISPLAY WREC
CLOSE IFILE2.
Assume that IFILE contains the following data:
1234567890
123456789*123456789*
123456789*123456789*123456789*
The preceding program displays the following:
EXAMPLE 1 OCCURS DEPENDING ON REC
1234567890
+00010
123456789*123456789*
+00020
123456789*123456789*123456789*
+00030
EXAMPLE 2 FIXED REC
1234567890XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
EXAMPLE 3 READ INTO WREC
123456789*123456789*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
123456789*123456789*
MPE/iX 5.0 Documentation