.Write a program that merges the numbers in two files and writes all the numbers into a
third file. Your program takes input from two different files and writes its output to a third
file. Each input file contains a list of numbers of type int in sorted order from the smallest
to the largest. After the program is run, the output file will contain all the numbers in the
two input files in one longer list in sorted order from smallest to largest. Your program
should define a function that is called with the two input-file streams and the output-file
stream as three arguments.
The while loop in your mergeFile function is looping infinitely. If the last value read from one of your files is smaller than one in the other file, it could cause this to happen.
For instance, lets say in fs1 you have "3 5 7" and in fs2 you have "4 8 12". I'm just gonna go through step by step what your function will do:
1. next1 = 3
next2 = 4
2. Compare 3 < 4, returns true
3. Write 3 to fs3
next1 = 5
4. Compare 5 < 4, returns false
5. Write 4 to fs3
next2 = 8
6. Compare 5 < 8, returns true
7. Write 5 to fs3
next1 = 7
8. Compare 7 < 8, returns true
9. Write 7 to fs3
Attempt to put next value in fs1 into next1. However, there are no values left, so 7 will remain in next1.
10. Compare 7 < 8, returns true
11. Write 7 to fs3
Attempt to put next value in fs1 into next1. However, there are no values left, so 7 will remain in next1.
As you can hopefully see, steps 10 and 11 will now repeat forever. You need to add some way to check if one of your files is at the end.
Actually,when it jumps to the last item in fs1 (or fs2),the loop is stop already,there's no way to use it and compare with items in fs2 (or fs1).the while stop it because it reaches eof.
so some items were lost,and a compiled and the program ends normally => no infinite loop.
Could you check the code again ? I think another marker should do the trick.
I'm not wrong, it's looping infinitely. Your while loop will only terminate when the ends of BOTH files are reached, not just one of them. However, this will never happen due to what I said above. You can add some other checks for one end of file being reached, and if you do it right that should be able to fix it. Just play around with it for a while.