How to Read a Variable Length File in COBOL
IBM's Common Business Oriented Language (COBOL) programming language allows users to process different types of data in financial and business software applications. COBOL supports the variable-length records data type for use in database files stored on hard disks; the user specifies the maximum and minimum record lengths for describing disk files with variable-length records. Reading variable-length data from a file allows you to process database information without defining the exact size of a record in your COBOL program.
Instructions
-
-
1
Type "Edit" on the z/OS360 mainframe system-management console and press the "Enter" key to edit your COBOL program.
-
2
Add the following code to the beginning of your program:
IDENTIFICATION DIVISION.
PROGRAM-ID. COBVAR.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IFILE 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.
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 FIXED REC"
OPEN INPUT IFILE
MOVE ALL "X" TO IREC
READ IFILE AT END MOVE -1 TO LEN
DISPLAY IREC
DISPLAY SPACE
-
-
3
Type "Save" on the console and press the "Enter" key to enable your program to process variable-length records.
-
1
References
- Photo Credit Jupiterimages/Photos.com/Getty Images