0% found this document useful (0 votes)
167 views2 pages

Matching File

This program copies records from two input files to an output file while sorting the records by key. It opens the input and output files, reads the first record from each input file, then enters a loop to compare the keys and write matching or lower records to the output file until it reaches the end of one of the input files. It closes all files at the end.

Uploaded by

Die001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views2 pages

Matching File

This program copies records from two input files to an output file while sorting the records by key. It opens the input and output files, reads the first record from each input file, then enters a loop to compare the keys and write matching or lower records to the output file until it reaches the end of one of the input files. It closes all files at the end.

Uploaded by

Die001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

ID DIVISION.

PROGRAM-ID. FCOPY.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INFILE ASSIGN TO DD1
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS FSTAT1.
SELECT OUTFILE ASSIGN TO DD2
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS FSTAT2.

Data division.
FILE SECTION.
FD IN-FILE1.
01 IN-FILE1-REC.
10 KEY-FILE1 PIC 9(1).
FD IN-FILE2.
01 IN-FILE2-REC.
10 KEY-FILE2 PIC 9(1).
FD OUT-FILE3.
01 OUT-FILE3-REC.
10 KEY-FILE3 PIC 9(1).

WORKING-STORAGE SECTION.
01 WS-TEMP-VAR.
05 WS-FILE1-EOF-FLG PIC X(1) VALUE SPACE.
88 WS-FILE1-SOF VALUE 'N'.
88 WS-FILE1-EOF VALUE 'Y'.
05 WS-FILE2-EOF-FLG PIC X(1) VALUE SPACE.
88 WS-FILE2-SOF VALUE 'N'.
88 WS-FILE2-EOF VALUE 'Y'.

PROCEDURE DIVISION.

0000-BEGIN.
PERFORM 1000-INIT
PERFORM 2000-PROCESS THRU 2000-EXIT
UNTIL WS-FILE1-EOF OR WS-FILE2-EOF
PERFORM 3000-TERMINATE THRU 3000-EXIT
GOBACK.

1000-INIT.
OPEN INPUT IN-FILE1
IN-FILE2
OPEN OUTPUT OUT-FILE3
PERFORM READ-FILE1
PERFORM READ-FILE2.
****************************************************
2000-PROCESS.
IF KEY-FILE1 = KEY-FILE2
MOVE IN-FILE1-REC TO OUT-FILE3-REC
WRITE OUT-FILE3-REC
PERFORM READ-FILE1
PERFORM READ-FILE2
ELSE

IF KEY-FILE1 < KEY-FILE2


MOVE IN-FILE1-REC TO OUT-FILE3-REC
WRITE OUT-FILE3-REC
PERFORM READ-FILE1
PERFORM WRITE-FILE2

ELSE

MOVE IN-FILE2-REC TO OUT-FILE3-REC


WRITE OUT-FILE3-REC
PERFORM READ-FILE2
PERFORM WRITE-FILE1
END-IF
END-IF.
****************************************************
2000-EXIT.
EXIT.
****************************************************
READ-FILE1.
READ IN-FILE1
AT END
DISPLAY 'FILE1 FILE IS EMPTY'
SET WS-FILE1-EOF TO TRUE
END-READ.

****************************************************
READ-FILE2.
READ IN-FILE2
AT END
DISPLAY 'FILE2 FILE IS EMPTY'
SET WS-FILE2-EOF TO TRUE
END-READ.
****************************************************
WRITE-FILE1.
IF WS-FILE2-EOF
MOVE IN-FILE1-REC TO OUT-FILE3-REC
WRITE OUT-FILE3-REC
END-IF.
****************************************************
WRITE-FILE2.
IF WS-FILE1-EOF
MOVE IN-FILE2-REC TO OUT-FILE3-REC
WRITE OUT-FILE3-REC
END-IF.
*****************************************************
3000-TERMINATE.
CLOSE IN-FILE2
IN-FILE1
OUT-FILE3
3000-EXIT.
EXIT.

You might also like