I need to take the numbers listed in the two files "numbers1.txt" and "numbers2.txt" (where the numbers are listed in ascending order) and move them to a file named "output.txt" where they are arranged in ascending order.
Here is what I have so far:
void appendToOutput(FILE *numFile1, FILE *numFile2, FILE *outputFile)
{
int num1 = 0;
int num2 = 0;
do {
fscanf(numFile1, "%d", &num1);
fscanf(numFile2, "%d", &num2);
while ((num1 < num2) && !feof(numFile1))
{
fprintf(outputFile, "%d\n", num1);
fscanf(numFile1, "%d", &num1);
}
while ((num2 < num1) && !feof(numFile2))
{
fprintf(outputFile, "%d\n", num2);
fscanf(numFile2, "%d", &num2);
}
if (num1 == num2)
{
fprintf(outputFile, "%d\n%d\n", num1, num2);
}
} while (!feof(numFile1) || !feof(numFile2));
return;
}
my files look like the following:
numbers1.txt
1
2
3
4
5
6
7
8
9
10
11
12
numbers2.txt:
2
4
6
8
10
12
14
16
18
20
22
24
The issue I am having is that the output file ends up looking like this: output.txt
1
2
2
3
4
4
5
6
6
7
8
8
9
10
10
11
12
12
So my issue is that the program is not continuing to read/write numbers from numbers2.txt even though it has not yet hit the end of it's file. I've looked through it and I can't seem to find out why it's stopping, so help would be appreciated!