Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing two sequential Files

Status
Not open for further replies.

nikos

MIS
May 19, 2000
5
US
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 &quot;non-comparable&quot; 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>&nbsp;&nbsp;OPEN INPUT FILE1 FILE2. (output file3)<br><br>&nbsp;&nbsp;MOVE 0 TO END1 END2.<br><br>PERFORM COMPARE-IT UNTIL END1 = 1 OR END2 = 1.<br><br>THE-END.<br>&nbsp;&nbsp;CLOSE FILE1 FILE2. (file3)<br>&nbsp;&nbsp;STOP RUN.<br><br>COMPARE-IT.<br>&nbsp;&nbsp;READ FILE1 AT END MOVE 1 TO END1.<br>&nbsp;&nbsp;READ FILE2 AT END MOVE 1 TO END2.<br>&nbsp;&nbsp;IF RECORD-1 NOT = RECORD-2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DISPLAY &quot;*** file1: ***&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DISPLAY RECORD-1.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DISPLAY &quot;-------------------------&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DISPLAY &quot;*** file2: ***&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 &quot;white space&quot; (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>&nbsp;&nbsp;&nbsp;Move each record to a single working storage area of the length of the record&nbsp;&nbsp;File-1-rec and&nbsp;&nbsp;File-2-rec&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Also define the Key File-1-rec-key) <br><br>Set up Loop<br><br>Evaluate True<br><br>&nbsp;&nbsp;&nbsp;When File 1 End of File<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write file 2 until End of File<br>&nbsp;&nbsp;&nbsp;When File 2 End of File&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write file 2 until End of File<br><br>&nbsp;&nbsp;&nbsp;When File-1-rec-key = File-2-rec-key&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If File-1-rec = File-2-rec&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write record same file<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else write differance file&nbsp;&nbsp;with both records or the record of choice<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end-if<br><br>&nbsp;&nbsp;&nbsp;When file-1-rec-key &gt; File-2-rec-key<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write differance file&nbsp;&nbsp;with both records or the record of choice<br><br>&nbsp;&nbsp;&nbsp;When file-1-rec-key &lt; File-2-rec-key<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write differance file&nbsp;&nbsp;with both records or the record of choice<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Greg&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;<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>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top