Working with Variable Length File
Fixed length files are the ones that have fixed record lenth.
ie if file is specified with the record width of 80 then all the records will
have same width.
However there can be files with variable record length.
On mainframes the file defined with “VB” or “V” as the record
format are all variable length file. These are generally delimited file which
means files in which fields don’t start at predefined positions, instead fields
are separated by some delimiter like pipe ( | ) etc.
For Variable length files the FD (file descriptor ) is quite
different.
We need to use the “RECORD IS VARYING IN SIZE [[FROM
Smallestsize] [TO largestsize] CHARACTERS] [ DEPENDING ON Recordsize]” in the FD entry.
Example:
FD FILENAME
RECORDING MODE IS V
RECORD IS VARYING IN SIZE
FROM 20 TO 180 CHARACTERS
FROM 20 TO 180 CHARACTERS
DEPENDING ON Variable01
01 InputRec PIC X(180).
Variable01 in the DEPENDING ON phase must be
an elementary unsigned integer data-item declared in the WORKING-STORAGE SECTION.
Ø When a record is read from the file , defined with a “RECORD
IS VARYING IN SIZE…DEPENDING ON” , the size of the record read into the buffer
is moved into the data-item Variable01
Ø To write a record to a variable length file we need to with
the RECORD IS VARYING IN SIZE.. DEPENDING ON phrase, the size of the record to be written must
first be moved to Variable01 data-item, and then the WRITE statement
must be executed.
Example: READ DATA FROM A VARIABLE LENTH FILE AND DISPLAY THE
DATA ALONG WITH THE LENGTH OF EACH OF THE RECORD.
Cobol Code:
***************************** Top of Data
******************************
*****************************************************************
*
*
* (1) IDENTIFICATION
DIVISION *
* *
*****************************************************************
IDENTIFICATION
DIVISION.
PROGRAM-ID. COBOLP8.
AUTHOR. SUKUL MAHADIK.
DATE-WRITTEN. 22/06/2012.
DATE-COMPILED.
SECURITY. COMPANY CONFIDENTIAL.
*****************************************************************
* LEARNING
DECLARATIVES.
*****************************************************************
ENVIRONMENT
DIVISION.
*****************************************************************
CONFIGURATION
SECTION.
*****************************************************************
SOURCE-COMPUTER.
IBM-370.
OBJECT-COMPUTER.
IBM-370.
*****************************************************************
INPUT-OUTPUT
SECTION.
*****************************************************************
FILE-CONTROL.
SELECT
INPUT1 ASSIGN TO INPUT1
ORGANIZATION IS SEQUENTIAL
* ACCESS MODE IS SEQUENTIAL
FILE STATUS
IS WS-INPUT1-STATUS.
DATA
DIVISION.
FILE
SECTION.
*THERE IS NO - BETWEEN WORD FILE AND SECTION
FD INPUT1
RECORDING
MODE IS V
RECORD IS
VARYING IN SIZE
FROM 10 TO
80 DEPENDING
ON
WS-RECD-LEN.
01 INPUT-RECORD PIC X(80).
*NOTE THAT WE HAVE NOT PROVIDED INDIVIDIAL FIELD DESCRIPTION.
*
AS FIELDS ARE SEPARATED BY DELIMITER,WE CANNOT PREDICT LENGTH
*
OF EACH OF THE FIELDS AND HENCE WE JUST WRITE THE SINGLE
*
RECORD NAME AT 01 LEVEL WITH DESCRIPTION OF MAXIMUM
*
RECORD LENGTH IN THE VB FILE.
WORKING-STORAGE
SECTION.
01 WS-RECD-LEN
PIC 9999.
01
WS-INPUT1-STATUS PIC XX.
01 WS-EOF-IND
PIC X(30).
88
ENDREACHED VALUE
"ENDREACHED".
PROCEDURE
DIVISION.
MAIN-PARA.
OPEN INPUT
INPUT1.
PERFORM
1000-READ-PARA THRU 1000-EXIT UNTIL ENDREACHED
CLOSE
INPUT1.
STOP
RUN.
1000-READ-PARA.
READ
INPUT1
AT
END
SET ENDREACHED
TO TRUE
GO TO
1000-EXIT
NOT AT
END
DISPLAY
"RECORD:" INPUT-RECORD(1:WS-RECD-LEN)
DISPLAY
"RECORD LENGTH:"
WS-RECD-LEN
*AS
SOON AS EACH RECORD IS READ , THE LENGH OF THAT RECORD IS POPULATED IN THE
FIELD WS-RECD- LEN
*
THIS WS VARIABLE WS-RECD-LEN IS USED ALONG WITH REFERENCE MODIFICATION TO
DISPLAY THE RECORD. IF WE DON’T USE THE REFERENCE MODIFICATION THEN THE DISPLAY
STATEMENT CONTINUES TO DISPLAY 80 CHARACTERS ON THE SPOOL EVEN IF THEY COME
FROM THE NEXT RECORD. I DON’T KNOW THE REASON WHY. THUS IT IS RECOMMEND THAT WE
USE REFERENCE MODIFICTION ALONG WITH LENGTH OF THE RECORD TO DISPLAY THE
RECORD.
DISPLAY
"============================"
END-READ.
1000-EXIT.
EXIT.
FOlLOWING IS THE TEST
JOB USED FOR RUNNING THE PROGRAM:
***************************** Top of Data
******************************
//TESTJOB1 JOB (EWDS),'READING SEQ FILE',NOTIFY=SM017R
//*****************************************************************
//STEO0001 EXEC PGM=COBOL08
//*****************************************************************
//STEPLIB DD DSN=CMN.XXXX.XXXX.#001542.LOD,DISP=SHR
//INPUT1 DD DSN=MVS001.TEST.VBFILE,DISP=SHR
//SYSABEND DD SYSOUT=*
//SYSOUT DD
SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//SYSABOUT DD SYSOUT=*
**************************** Bottom of Data
****************************
Input file MVS001.TEST.VBFILE is a VB file with a maximum length of 80
characters (declared with 84 , 4 extra types for record length ) and is having
following data:
****** ***************************** Top of Data
******************************
000001 0000000001|mumbai|pune|XYZ1234
000002 0000000002|ahmendabad|goa|24232bj00
000003 0000000003|delhi|newyork|12asj29
000004 000000004|kanpur|lucknow|2131hjk
****** **************************** Bottom of Data
****************************
The program is used to read the variable length file and display the record and the length of
each of the record.
Following is the
output of the program as seen in the SSO:
***************************** Top of Data
******************************
RECORD:0000000001|mumbai|pune|XYZ1234
RECORD
LENGTH:0030
============================
RECORD:0000000002|ahmendabad|goa|24232bj00
RECORD
LENGTH:0035
============================
RECORD:0000000003|delhi|newyork|12asj29
RECORD
LENGTH:0032
============================
RECORD:000000004|kanpur|lucknow|2131hjk
RECORD
LENGTH:0032
============================
**************************** Bottom of Data
****************************
If we don’t Use reference modification to
display the VB record, following is how it looks in the spool:
****** ***************************** Top of
Data ******************************
000001
RECORD:0000000001|mumbai|pune|XYZ1234
0000000002|ahmendabad|goa|2423
000002
RECORD LENGTH:0030
000003
============================
000004
RECORD:0000000002|ahmendabad|goa|24232bj00 0000000003|delhi|newyork|
000005
RECORD LENGTH:0035
000006
============================
000007
RECORD:0000000003|delhi|newyork|12asj29 000000004|kanpur|lucknow|213
000008
RECORD LENGTH:0032
000009
============================
000010
RECORD:000000004|kanpur|lucknow|2131hjk
000011
RECORD LENGTH:0032
000012
============================
****** **************************** Bottom of
Data ****************************
AND I DON’T KNOW THE REASON WHY IT DOES SO. I
THINK IT TRIES TO DISPLAY ALL THE 80 BYTES THAT ARE DEFINED IN THE INPUT-REC
DESCRIPTION.
No comments:
Post a Comment