I do not think it is possible to do all three tasks with your current code. Also what would happen if the files contained 1,000,000 records. You would be reading a file 1,000,000 times. It would take forever to finish (assuming the hard-disks survived).
For this type of processing I would suggest a sort-merge process:
- Sort the files into key sequence
- Do a merge on the 2 files (for 3 files the process is similar, you just have more complicated evaluate).
The processing logic for 2 files in key sequence becomes:
while not eof
evaluate true
when key-file1 < key-file2
Write file1-record to output-file-2
read file1
when key-file1 > key-file2
Write file2-record to output-file-3
read file2
when key-file1 = key-file2
/* match processing,
will involve reading at least one of the files */
move key-file1 to hold-key
while key-file1 = hold-key
Write Output-File-1
read file1
end
while key-file2 = hold-key
read file2
end
end
end
There will be End-of-file logic to add in
Note: It is unclear from the question how multiple entries for the same key should be handled