0

I created this little program which reads two files with sorted numbers and saves them into a third file. The numbers in "sorted1.txt" are "10 20 30 40 50 60 70" and in "sorted2.txt" - "5 15 25 35 45 55 65 75 85 95 105". When I compile and run the program, though, the last compared numbers are 60 and 65, while they should be 70 and 75. Moreover, in the "sorted3.txt" all the numbers are sorted, but 70 and 105 are missing. Can somebody tell me where the code breaks?

int main() {
  
    FILE *fp;
    FILE *fp1;
    FILE *fp2;
    FILE *fp3;

    int num, num1, num2;

    fp1 = fopen("sorted1.txt", "r");
    fp2 = fopen("sorted2.txt", "r");
    fp3 = fopen("sorted3.txt", "w");

    if(fp1 == NULL || fp2 == NULL) {
        printf("File not present!");
        exit(1);
    }
    fscanf(fp1, "%d", &num1);
    fscanf(fp2, "%d", &num2);

    while( !feof(fp1) && !feof(fp2) ) {
        printf("\nComparing %d and %d", num1, num2);
        if(num1 < num2) {
            fprintf(fp3, "%d ", num1);
            printf("\n%d", num1);
            fscanf(fp1, "%d", &num1);
        } else {
            fprintf(fp3, "%d ", num2);
            printf("\n%d", num2);
            fscanf(fp2, "%d", &num2);
        }
    }

    if(feof(fp1)) {
        fp = fp2;
        num = num2;
    }
    else if(feof(fp2)) {
        fp = fp1;
        num = num1;
    }
    while(!feof(fp)) {
        fprintf(fp3, "%d ", num);
        fscanf(fp, "%d", &num);
    }

    fclose(fp1);
    fclose(fp2);
    fclose(fp3);

    return 0;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
zaro
  • 75
  • 5
  • 1
    You are using `feof` incorrectly. https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – William Pursell Mar 17 '21 at 22:01
  • Where exactly? I recreated the example and although it is 1:1, something still breaks?=. – zaro Mar 17 '21 at 22:07
  • After successfully reading the last number with `scanf`, you abort instead of processing the last number, because `feof` is true at that point . (Note that in general it may or may not be true after reading the last item in the file, which is why it is not recommended to use `feof` to detect read failure). – M.M Mar 17 '21 at 22:08
  • Can't reproduce. Is this the **exact** code that is having the problem? – dbush Mar 17 '21 at 22:10
  • The code's the same. I just renamed the variables. – zaro Mar 17 '21 at 22:10
  • @dbush try having your file end on the last digit (i.e. no trailing whitespace or newline) – M.M Mar 17 '21 at 22:10
  • @M.M how does happen when the second file has 3 more numbers and it can't stop until they are read as well? – zaro Mar 17 '21 at 22:12
  • @zaro So it's *not* the same. Copy/paste the **exact** code that has the problem. – dbush Mar 17 '21 at 22:12
  • @dbush it is from a video. I mean what could be wrong when I only changed tmp, tmp1 and tmp2 to num, num1, num2? – zaro Mar 17 '21 at 22:14
  • @M.M Ah, that did it. – dbush Mar 17 '21 at 22:14
  • @zaro as explained earlier, the code is wrong and may behave differently on different systems . – M.M Mar 17 '21 at 22:14
  • @M.M since feof is bad practice, how should I watch out for end of file? – zaro Mar 17 '21 at 22:17
  • @zaro please see the link in the first comment , by William Pursell . Instead of checking for end of file, you should check for reading a number succeeding or failing. – M.M Mar 17 '21 at 22:17
  • @M.M ok. But I can't understand if feof is so bad, why it has been created in the first place and some people still use it? – zaro Mar 17 '21 at 22:19
  • People still use it because they copy code they saw on the internet without understanding it. It can be used if you want to find out why your read failed (i.e. whether it failed due to end-of-file, or due to some other error). C is made up of mostly low level primitive operations and sometimes it requires combining a few such operations, or using a different one than you first thought of, in order to get the intended high level behaviour – M.M Mar 17 '21 at 22:23
  • @M.Mthank you very much. I will check out the link, although I am confused how many people use it and my professor taught us to use it. – zaro Mar 17 '21 at 22:27

0 Answers0