[USflag] The American Programmer [USflag]


Home
Books on Mainframe Programming
Mainframe Manuals and Tutorials
System Abend codes, Sqlcodes, VSAM/QSAM codes
Everything about the IBM AS/400 Midrange Computer - iSeries
Everything about CICS
Cobol programs, manuals, books
  Sample Cobol code: The Simple, Single File COBOL Program
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Sequence Check COBOL Program
  Sample Cobol code: The Record Selection COBOL Program
  Sample Cobol code: The Edit or Validate COBOL Program
  Sample Cobol code: The The One Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Three Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Sequential File, Batch Update COBOL Program
  Sample Cobol code: The COBOL Sort
  Sample Cobol code: The CASE Structure: COBOL EVALUATE
  Sample Cobol code: Direct Subscripting in COBOL
  Sample Cobol code: The Sequential, or Serial Search in COBOL
  Sample Cobol code: The Binary Search in COBOL
  Sample Cobol code: Loading a Table from a Sequential File in a COBOL program
  Sample Cobol code: The VSAM File Read Sequentially in a COBOL program
  Sample Cobol code: The VSAM KSDS, Read Randomly in a COBOL program
  Sample Cobol code: The VSAM File, Read Randomly in a COBOL program
  Sample Cobol code: VSAM Initial Load in a COBOL program
  Sample Cobol code: VSAM File Maintenance (Add, Change, Delete) in a COBOL program
  Sample Cobol code: VSAM Read Sequentially, with START, in a COBOL program
  Sample Cobol code: Creating a Variable Format File in a COBOL program
  Sample Cobol code: Reading a Variable Format File in a COBOL program
  Sample Cobol code: Creating a Variable Format File with Occurs Depending On in a COBOL program
  Sample Cobol code: COBOL Reading a Variable Format File with Occurs Depending On, in a COBOL program
  Sample Cobol code: COBOL The Table Load with Occurs Depending On
  Sample Cobol/DB2 code: Singleton Select
  Manuals on the COBOL programming language.
  Books on Cobol
  Abend Codes from Cobol programs
Everything about DB2 and SQL
Everything about IMS
Everything about Java and JavaScript
Everything about JCL and JES
Everything about REXX
Everything about zOS, VSAM, Tivoli, Assembler
Everything about TSO, ISPF, Spufi
Site Map and Site Search

           Home   > COBOL   > COBOL program Reading a Variable Format File

Reading a Variable Format File in a COBOL program


23. Reading a Variable Format File

VARREAD1.
This program will read the variable format file created by the previous program, VARWRIT1. The secret to this is that you READ first, then check the record type field to see what kind of record you just got.
Then move the record to the proper length working-storage data item.

Here is the program VARREAD1:

Compliments of Gabe Gargiulo, author of several recent books on programming and modern languages, available at Amazon.com.



000200 IDENTIFICATION DIVISION.
000300 PROGRAM-ID. VARREAD1.
000400* the input file is variable format
000500*     with three record lengths: 20, 40, 60
000600* you must read first,
000700* before you know if you have short, med, long
000800*
000900 ENVIRONMENT DIVISION.
001000 CONFIGURATION SECTION.
001100 INPUT-OUTPUT SECTION.
001200 FILE-CONTROL.
001300*  INPUT FILE: the one created by varwrit1 program
001400     SELECT INFILE  ASSIGN VARINP.
001800     SELECT OUTFILE ASSIGN REPORTFI.
002200 DATA DIVISION.
002300 FILE SECTION.
002400
002500 FD  INFILE
002510     RECORDING MODE IS V
002700*     MVS adds 4 to the maximum record length you put here
002800*     so 64 is really the maximum record length.
002900*     don't put  64 here, because MVS would make it 68...
003000*     if you use the LRECL parameter in JCL, it must be 64 , not
003100*     (the minimum record length means nothing -
003200*           you could put 0, 1, or 59...they all work the same)
003300     RECORD CONTAINS 20 to 60 CHARACTERS.
003600*     note the three record descriptions.
003700*     you don't know which record type
003800*      you have read until you check the indicator
003900*         (the literal in the first 9 positions)
004000 01  WS-SHORT-RECORD.
004100*     record is only 20 characters long
004200      05  RECORD-TYPE  PIC X(10).
004300      05  SHORT-NAME-1 PIC X(10).
004400
004500 01  WS-MEDIUM-RECORD.
004600*     record is only 40 characters long
004700      05  filler        PIC X(10).
004800      05  MEDIUM-NAME-1 PIC X(10).
004900      05  MEDIUM-NAME-2 PIC X(10).
005000      05  MEDIUM-NAME-3 PIC X(10).
005100
005200 01  WS-LONG-RECORD.
005300**     record is only 60 characters long
005400      05  filler      PIC X(10).
005500      05  LONG-NAME-1 PIC X(10).
005600      05  LONG-NAME-2 PIC X(10).
005700      05  LONG-NAME-3 PIC X(10).
005800      05  LONG-NAME-4 PIC X(10).
005900      05  LONG-NAME-5 PIC X(10).
006000
006100 FD  OUTFILE
006110     RECORDING MODE IS F
006400     RECORD CONTAINS 133 CHARACTERS.
006500 01  OUT-RECORD PIC X(133).
006600
006700 WORKING-STORAGE SECTION.
006800 01  SWITCHES.
006900      05  FILE-AT-END     PIC X  VALUE 'N'.
007000
007100* 01  RECORD-COUNT          PIC S9(7) PACKED-DECIMAL VALUE +0.
007200* 01  DISPLAY-RECORD-COUNT  PIC Z(6)9.
007300
007400 01  WS-PRINT-LINE.
007500     05 FILLER         PIC X     VALUE SPACES.
007600     05 PRINT-NAME-1   PIC X(10) VALUE SPACES.
007700     05 FILLER         PIC X     VALUE SPACES.
007800     05 PRINT-NAME-2   PIC X(10) VALUE SPACES.
007900     05 FILLER         PIC X     VALUE SPACES.
008000     05 PRINT-NAME-3   PIC X(10) VALUE SPACES.
008100     05 FILLER         PIC X     VALUE SPACES.
008200     05 PRINT-NAME-4   PIC X(10) VALUE SPACES.
008300     05 FILLER         PIC X     VALUE SPACES.
008400     05 PRINT-NAME-5   PIC X(10) VALUE SPACES.
008500
008700 PROCEDURE DIVISION.
008800     PERFORM INITIALIZATION
008900     PERFORM PROCESS-ALL
009000**       UPPER CASE Y, PLEASE
009100         UNTIL FILE-AT-END = 'Y'
009200     PERFORM TERMINATION
009300     GOBACK.
009400
009500 INITIALIZATION.
009600     OPEN INPUT INFILE
009700          OUTPUT OUTFILE
009800     PERFORM READ-PAR.
009900
010000 PROCESS-ALL.
010100      EVALUATE RECORD-TYPE
010200       WHEN 'SHORT' PERFORM HAVE-SHORT-RECORD
010300       WHEN 'MEDIUM' PERFORM HAVE-MEDIUM-RECORD
010400       WHEN 'LONG' PERFORM HAVE-LONG-RECORD
010500       END-EVALUATE
010600
010700     PERFORM READ-PAR.
010800
010900 HAVE-SHORT-RECORD.
011000        MOVE SPACES TO  WS-PRINT-LINE.
011100        MOVE SHORT-NAME-1 TO PRINT-NAME-1
011200        WRITE OUT-RECORD FROM WS-PRINT-LINE
011300              AFTER ADVANCING 1 LINE.
011400
011500 HAVE-MEDIUM-RECORD.
011600        MOVE SPACES TO  WS-PRINT-LINE.
011700        MOVE MEDIUM-NAME-1 TO PRINT-NAME-1
011800        MOVE MEDIUM-NAME-2 TO PRINT-NAME-2
011900        MOVE MEDIUM-NAME-3 TO PRINT-NAME-3
012000        WRITE OUT-RECORD FROM WS-PRINT-LINE
012100               AFTER ADVANCING 1 LINE.
012200
012300 HAVE-LONG-RECORD.
012400        MOVE SPACES TO  WS-PRINT-LINE.
012500        MOVE LONG-NAME-1 TO PRINT-NAME-1
012600        MOVE LONG-NAME-2 TO PRINT-NAME-2
012700        MOVE LONG-NAME-3 TO PRINT-NAME-3
012800        MOVE LONG-NAME-4 TO PRINT-NAME-4
012900        MOVE LONG-NAME-5 TO PRINT-NAME-5
013000        WRITE OUT-RECORD FROM WS-PRINT-LINE
013100               AFTER ADVANCING 1 LINE.
013200
013300 TERMINATION.
013400     CLOSE INFILE OUTFILE.
013500
013600 READ-PAR.
013700     READ INFILE
013800         AT END MOVE 'Y' TO FILE-AT-END
013900     END-READ.

Compliments of Gabe Gargiulo, author of several recent books on programming and modern languages, available at Amazon.com.

Here is sample JCL: //STEP1 EXEC PGM=VARWRIT1 //STEPLIB DD DSN=your.executable.program.library.goes.here,DISP=SHR //*OF COURSE, THE NEXT LIBRARY NAME MAY BE DIFFERENT AT YOUR COMPANY //FIXEDINP DD DSN=userid.COBBOOK.DATA(FIXEDINP),DISP=SHR //VAROUT DD DSN=&&TEMPFILE, // DISP=(NEW,PASS), // UNIT=SYSDA, MAY HAVE TO CHANGE SYSDA // SPACE=(TRK,(1)), // LRECL=64,RECFM=VB 64 is right - programís 60, +4 //SYSOUT DD SYSOUT=* //SYSUDUMP DD SYSOUT=* //* //STEP2 EXEC PGM=VARREAD1 //STEPLIB DD DSN=your.executable.program.library.goes.here,DISP=SHR //*OF COURSE, THE NEXT LIBRARY NAME MAY BE DIFFERENT AT YOUR COMPANY //VARINP DD DSN=&&TEMPFILE,DISP=(OLD,DELETE), // LRECL=64,RECFM=VB //REPORTFI DD SYSOUT=* //SYSOUT DD SYSOUT=* //SYSUDUMP DD SYSOUT=*

Top of Page

Your email and other personal information will not be given to anyone
[Books Computer]

Home Books for Computer Professionals Privacy Terms |
Site Map and Site Search Programming Manuals and Tutorials The REXX Files Top of Page |