How to swap them is the tricky part. |
It's really not that tricky. But it might be easier to do a little manual experimentation.
Since your demo files each have three numbers take 6 pieces of paper with each number and place them in two piles on your desk. Each stack is sorted lowest on top highest on bottom.
1. Now read each file (pick up the top number).
2. Compare the two numbers in your hand.
3. Place the lower of the two on the desk in your "output" pile.
4. Now fill your empty hand (read the corresponding file) with a number from the corresponding pile.
5. Repeat until you can't read from one of the files (one of the piles is empty). (loop to step 2.)
6. Now you need to write the value remaining in your hand to the output file.
7. Now you probably have one or more numbers left in one of your piles that need to be read and then written to the output file. This can be accomplished by reading both input files and writing the values read to the output file.
Remember when the file is empty it triggers the eof() flag on the stream so that stream will no longer read anything.