Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Telling wether two files are the same in C, but cannot tell the fallacy of this algorithm

My goal is to make a function telling wether two different files are the same (they contain the same content).

This is my code:

int are_files_equal(char* filename1, char* filename2){  
if(strcmp(filename1, filename2) == 0)
        return 1;

    struct stat stat1, stat2;

    if ( stat(filename1, &stat1) != 0 || stat(filename2, &stat2) != 0)
        return -1; // error opening files

    if(stat1.st_size != stat2.st_size)
        return 0; // files are not the same as they have a different dimension
    
    FILE *file1 = fopen(filename1, "rb");
    FILE *file2 = fopen(filename2, "rb");
    if (file1 == NULL || file2 == NULL)
    {
        return -1; // error opening files
    }
    #define BYTES_TO_READ_AT_ONCE 512000
    unsigned char databuffer1[BYTES_TO_READ_AT_ONCE];
    unsigned char databuffer2[BYTES_TO_READ_AT_ONCE];
    size_t bytes;
    while ((fread(databuffer1, 1, BYTES_TO_READ_AT_ONCE, file1)) != 0)
    {
        fread(databuffer2, 1, BYTES_TO_READ_AT_ONCE, file2);
        if(memcmp(databuffer1, databuffer2, BYTES_TO_READ_AT_ONCE) != 0)
            return 0;
    }
    fclose(file1);
    fclose(file2);
    return 1;
}

But for some reason I am not able to understand, it fails. In particular the if(memcmp(databuffer1, databuffer2, BYTES_TO_READ_AT_ONCE) != 0) holds true. Why does it happen? Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are not checking the correct buffer length. Try

size_t bytes;
bool matching = true;
while ((bytes = fread(databuffer1, 1, BYTES_TO_READ_AT_ONCE, file1)) != 0)
{
    if(bytes != fread(databuffer2, 1, BYTES_TO_READ_AT_ONCE, file2) ||
            memcmp(databuffer1, databuffer2, bytes) != 0) {
        matching = false;
        break;
    }
}

if(fread(databuffer2, 1, BYTES_TO_READ_AT_ONCE, file2) != 0) {
    matching = false;    // check if the whole file was read
}
fclose(file1);
fclose(file2);
return matching;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading