[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 Creating a Variable Format File

Creating a Variable Format File in a COBOL program


22. Creating a Variable Format File

VARWRIT1.
This program creates a variable format file. The records in the file are of different lengths. This will save space on the disk or tape file where they are stored.

The secret to this is in the WRITE statement. To create a short record, you WRITE the short record. To create a medium, you WRITE the medium, etc.

If you simply send the output file to the printer, you won’t know if you have actually created a variable format file. You must pass it to another program which will try to read it.
We will do that with the JCL. So the JCL used with this program will not be shown until after the program that reads the file, VARREAD1.
This will allow you to create both programs and then test them together in one set of JCL.

This set of two programs, VARWRIT1 and VARREAD1 doesn’t do anything very complex. They simply create and use a variable format file.
They will work properly only if you actually create a valid variable format file.

Here is the program VARWRIT1:

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



000200 IDENTIFICATION DIVISION.
000300 PROGRAM-ID. VARWRIT1.
000400* the input file is fixed format
000500* the output file is variable format
000600*     with three record lengths: 20, 40, 60
000700* this will create variable format records
000800* you won't be able to prove that they are really variable format
000900*   until you read the file with another program
001200 ENVIRONMENT DIVISION.
001300 CONFIGURATION SECTION.
001400 INPUT-OUTPUT SECTION.
001500 FILE-CONTROL.
001600*  INPUT FILE: fixedinp
001700     SELECT INFILE  ASSIGN FIXEDINP.
002100     SELECT OUTFILE ASSIGN VAROUT.
002500 DATA DIVISION.
002600 FILE SECTION.
002700 FD  INFILE
002810     RECORDING MODE IS F
003100     RECORD CONTAINS 80 CHARACTERS.
003110 01  IN-RECORD.
003120*     ALL RECORDS ARE SAME LENGTH - SO SPACE WASTED
003130      05  IN-RECORD-TYPE PIC X(10).
003140      05  IN-NAME-1      PIC X(10).
003150      05  IN-NAME-2      PIC X(10).
003160      05  IN-NAME-3      PIC X(10).
003170      05  IN-NAME-4      PIC X(10).
003180      05  IN-NAME-5      PIC X(10).
003190      05  FILLER         PIC X(20).
003200
003400 FD  OUTFILE
003410     RECORDING MODE IS V
003600     RECORD CONTAINS 20 to 60 CHARACTERS.
003700*     MVS adds 4 to the maximum record length you put here
003800*     so 64 is really the maximum record length.
003900*     don't put  64 here, because MVS would make it 68...
004000*     if you use the LRECL parameter in JCL, it must be 64 , not
004100*     (the minimum record length means nothing -
004200*           you could put 0, 1, or 59...they all work the same)
004500*     note the three record descriptions.
004600*     the way you create a short record is to say
004700*      write short-record from ....
004800*     the way you create a medium record is to say
004900*      write medium-record from ....
005000*     the way you create a long record is to say
005100*      write long-record from ....
005200*      these can be in any order ....
005300 01  SHORT-RECORD  PIC X(20).
005400 01  MEDIUM-RECORD PIC X(40).
005500 01  LONG-RECORD   PIC X(60).
005600
005700 WORKING-STORAGE SECTION.
005800 01  SWITCHES.
005900      05  FILE-AT-END     PIC X  VALUE 'N'.
006000
006100*01  RECORD-COUNT          PIC S9(7) PACKED-DECIMAL VALUE +0.
006200*01  DISPLAY-RECORD-COUNT  PIC Z(6)9.
006300
007400 01  WS-SHORT-RECORD.
007500*     record is only 20 characters long
007600      05  SHORT-RECORD-TYPE PIC X(10).
007700      05  SHORT-NAME-1      PIC X(10).
007800*   no fillers needed
007900
008000 01  WS-MEDIUM-RECORD.
008100*     record is only 40 characters long
008200      05  MEDIUM-RECORD-TYPE PIC X(10).
008300      05  MEDIUM-NAME-1      PIC X(10).
008400      05  MEDIUM-NAME-2      PIC X(10).
008500      05  MEDIUM-NAME-3      PIC X(10).
008600*   no fillers needed
008700
008800 01  WS-LONG-RECORD.
008900**     record is only 60 characters long
009000     05  LONG-RECORD-TYPE PIC X(10).
009100      05  LONG-NAME-1 PIC X(10).
009200      05  LONG-NAME-2 PIC X(10).
009300      05  LONG-NAME-3 PIC X(10).
009400      05  LONG-NAME-4 PIC X(10).
009500      05  LONG-NAME-5 PIC X(10).
009600*   no fillers needed
009700
009800 PROCEDURE DIVISION.
009900     PERFORM INITIALIZATION
010000     PERFORM PROCESS-ALL
010100**       UPPER CASE Y, PLEASE
010200         UNTIL FILE-AT-END = 'Y'
010300     PERFORM TERMINATION
010400     GOBACK.
010600 INITIALIZATION.
010700     OPEN INPUT  INFILE
010800          OUTPUT OUTFILE
010900     PERFORM READ-PAR.
011100 PROCESS-ALL.
011200*    don't really need to move spaces as in next line
011300*    except on microfocus cobol workbench
011400      MOVE SPACES TO LONG-RECORD
011500      EVALUATE IN-RECORD-TYPE
011600       WHEN 'SHORT'  PERFORM WRITE-SHORT-RECORD
011700       WHEN 'MEDIUM' PERFORM WRITE-MEDIUM-RECORD
011800       WHEN 'LONG'   PERFORM WRITE-LONG-RECORD
011900       END-EVALUATE
012000
012100     PERFORM READ-PAR.
012200
012300 WRITE-SHORT-RECORD.
012400        MOVE IN-RECORD-TYPE TO SHORT-RECORD-TYPE
012500        MOVE IN-NAME-1 TO SHORT-NAME-1
012600        WRITE SHORT-RECORD FROM WS-SHORT-RECORD.
012700
012800 WRITE-MEDIUM-RECORD.
012900        MOVE IN-RECORD-TYPE TO MEDIUM-RECORD-TYPE
013000        MOVE IN-NAME-1 TO MEDIUM-NAME-1
013100        MOVE IN-NAME-2 TO MEDIUM-NAME-2
013200        MOVE IN-NAME-3 TO MEDIUM-NAME-3
013300        WRITE MEDIUM-RECORD FROM WS-MEDIUM-RECORD.
013400
013500 WRITE-LONG-RECORD.
013600        MOVE IN-RECORD-TYPE TO LONG-RECORD-TYPE
013700        MOVE IN-NAME-1 TO LONG-NAME-1
013800        MOVE IN-NAME-2 TO LONG-NAME-2
013900        MOVE IN-NAME-3 TO LONG-NAME-3
014000        MOVE IN-NAME-4 TO LONG-NAME-4
014100        MOVE IN-NAME-5 TO LONG-NAME-5
014200        WRITE LONG-RECORD FROM WS-LONG-RECORD.
014300
014400 TERMINATION.
014500     CLOSE INFILE OUTFILE.
014700 READ-PAR.
014800     READ INFILE 
014900         AT END MOVE 'Y' TO FILE-AT-END
015000     END-READ.

Compliments of Gabe Gargiulo, author of several recent books on programming and modern languages, available at Amazon.com.
Here is the input data file FIXEDINP: (the next two lines are a column ruler) 1 2 3 4 5 6 123456789.123456789.123456789.123456789.123456789.123456789.12345678 MEDIUM BETH KELLY ANTHONY LONG LAURA RICK NANCY MARIA ELLEN SHORT MOE MEDIUM NORMA DENISE JEAN LONG MAURA NICK SALLY MARIO ELLIE

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 |