0

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!

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
Beej
  • 1
  • 1
  • 2
    [Please see Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/2173917) – Sourav Ghosh Dec 06 '17 at 07:38
  • Have you tried stepping through with a debugger? Hint: After the end of `numFile1`, your second while loop is `while (14 < 12 && !feof()){}`. – Ken Y-N Dec 06 '17 at 07:45
  • @Ani The fix isn't that simple. That will prevent premature exit, but it will attempt to read past EOF on the shorter file. – Tom Karzes Dec 06 '17 at 08:12

1 Answers1

1

There are a few problems with the current code

  1. Initialization of the 2 numbers should be performed before the main loop
  2. Use of feof() here
  3. Test of strict < insteaf of <= (could lock your algo)
  4. Keep reading a file if the other one becomes empty

Regarding 2., fscanf returns a number >0 if it could read the number, so you can test this instead of checking feof (that says that EOF (a stop condition) happened before, and this might trigger one more unwanted iteration).

Suggested fixed code:

int read1 = fscanf(numFile1, "%d", &num1);
int read2 = fscanf(numFile2, "%d", &num2);

while (read1 > 0 || read2 > 0) {
    while (read1 > 0 && (read2<=0 || num1 <= num2))
    {
        fprintf(outputFile, "%d\n", num1);
        read1 = fscanf(numFile1, "%d", &num1);
    }
    while (read2 > 0 && (read1<=0 || num2 <= num1))
    {
        fprintf(outputFile, "%d\n", num2);
        read2 = fscanf(numFile2, "%d", &num2);
    }
} 

Further explanations

  • readX<=0 || numY <= numX addresses 4.
  • readX = fscanf(...) addresses2.
Déjà vu
  • 28,223
  • 6
  • 72
  • 100
  • When I tried to use `fscanf`'s return values the program didn't put anything into the output file. It only started working once I put in the `feof()` – Beej Dec 06 '17 at 10:26
  • This is because the excerpt above was to show the output in the console. The answer has been edited to write results into `outputFile`. – Déjà vu Dec 06 '17 at 12:43