Search code examples
cscanfchars

fscanf from two different files


During an assignment in school I came across this phenomenon which I cannot understand.

My task was to read two files and check wether they are exactly the same. I made two text files which contained the exact same line:

"Hello world"

I decided to check the text char by char. at first I wrote the following code:

EDIT: Due to many requests i've re-written the entire code to be displayed here:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    char c1, c2;
    int ans = 1;
    FILE *f1 = fopen("text1.txt","rt");
    FILE *f2 = fopen("text2.txt","rt");
    for (fscanf(f1, "%c", &c1), fscanf(f2, "%c", &c2); 
        !feof(f1) && !feof(f2) && ans; 
        fscanf(f1, "%c", &c1), fscanf(f2, "%c", &c2))
    { // Check Data:
    if (c1 != c2) ans = 0; 
    printf("%c %c\n",c1,c2); // Print side by side check
    } // Check Tail:
    if (!feof(f1)) ans=0;
    if (!feof(f2)) ans=0;

    if (ans) printf("File 1 == File 2");
    else printf("File 1 != File 2");

    return 0;
}

but for some reason the code entered 'H' into c1 and 'e' into c2. Why does it work like that?

EDIT: i cannot seem to replicate the problem (this happened to me during a test i took in the university, thus i cannot access the original code anymore. the university is using an outdated Microsoft Visual Studio 2012 while i code using the 2015express version/netbeans)


Solution

  • Looking at your code, I cannot see an explanation for the behavior you document. You should post a minimal complete verifiable example for use to see the rest of the function.

    Your approach is not very effective and will fail to detect some cases where files differ: the way you test for end of file is approximative.

    Here is an alternative using getc:

    int c1, c2;
    int identical = 1;
    
    for (;;) {
        c1 = getc(f1);
        c2 = getc(f2);
    
        if (c1 != c2) {
            identical = 0;
            break;
        }
        if (c1 == EOF)
            break;
    }
    

    EDIT: after you posted more code, you concluded: i cannot seem to replicate the problem (this happened to me during a test i took in the university, thus i cannot access the original code anymore.

    My guess is you were scanning both c1 and c2 from f1 in the initial part of the for loop, a classic cut and paste bug.