What is the best possible way to compare two sequential files in a COBOL program? I'm trying to create one file that contains matched data, and one file that contains items that did not match. Can anyone provide me with some pseudocode?
Hi,<br><br>It is not so easy to do comparing of two files.<br>First check your operating system utilities, for example:<br>for MS DOS/Windows use: FC file1 file2<br>for CompaqVMS use: Difference file1 file2<br>...<br>These utilities work intelligently, e.g. they don't match<br>each *pair* of records (one from file1 and one from file2)<br>but jump thru records which are "non-comparable" from<br>one file with records from another ... (it is very good<br>for comparing text files). There are also many switches<br>provided with them (especially for Diff for VAX/Alpha<br>VMS computers)<br>If you have to do comparing in your COBOL program,<br>try to read files alternatively:<br><br> OPEN INPUT FILE1 FILE2. (output file3)<br><br> MOVE 0 TO END1 END2.<br><br>PERFORM COMPARE-IT UNTIL END1 = 1 OR END2 = 1.<br><br>THE-END.<br> CLOSE FILE1 FILE2. (file3)<br> STOP RUN.<br><br>COMPARE-IT.<br> READ FILE1 AT END MOVE 1 TO END1.<br> READ FILE2 AT END MOVE 1 TO END2.<br> IF RECORD-1 NOT = RECORD-2<br> DISPLAY "*** file1: ***"<br> DISPLAY RECORD-1.<br> DISPLAY "-------------------------"<br> DISPLAY "*** file2: ***"<br> DISPLAY RECORD-2.<br>(YOU CAN ALSO USE WRITE RECORD-3 to file-3 if you want the differencies<br>written in a separate file)<br><br>note that this is simple checking. If your files are formatted it could work.<br>The real thing is to ignore or not to ignore "white space" (repetitive blanks,<br>tabs etc), to ignore upper and lower case etc.<br><br>Georgi, ;-)
Another way would be if the files are keyed,<br>(Psudo Code)<br>Sort both files on the same key<br>read 1 record from both files (Prime Read)<br> Move each record to a single working storage area of the length of the record File-1-rec and File-2-rec <br> (Also define the Key File-1-rec-key) <br><br>Set up Loop<br><br>Evaluate True<br><br> When File 1 End of File<br> write file 2 until End of File<br> When File 2 End of File <br> write file 2 until End of File<br><br> When File-1-rec-key = File-2-rec-key <br> If File-1-rec = File-2-rec <br> write record same file<br> else write differance file with both records or the record of choice<br> end-if<br><br> When file-1-rec-key > File-2-rec-key<br> write differance file with both records or the record of choice<br><br> When file-1-rec-key < File-2-rec-key<br> write differance file with both records or the record of choice<br> <br> End-Evaluate<br><br><br><br>This code works very well and effeciently ( I bult a file compare just this way. 450 byte file structure and the whole compare code with comments was 26 lines.<br><br>remember the Evaluate criteria that it executes only the first condition that is satisfied, which is why the EOF check are first.<br><br>Hope this helps<br> Greg <br> <br> <br><br><br> <p>Greg Amos<br><a href=mailto:amosgreg@ix(dot)netcom(dot)com replace(dot)>amosgreg@ix(dot)netcom(dot)com replace(dot)</a><br><a href= > </a><br>
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.