2

I know how to check if two files have the same content. I made it using fopen and fread. However, I have some problems translating the code and use open and read instead of fopen and fread.

When I run the program like this: ./app file1.txt file2.txt (and files are not equal) program shows the message that they are not equal, and thats perfectly correct. When I try ./app file1 file1 instead, it also shows that the files are NOT equal, which is not true for sure. Another thing is that using Code::Blocks it shows me the return code 255 so I thing I messed something up, but do not what exactly.

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>

int CompareFileByteByByte(char *file1, char *file2)
{
    int results = 0, c1, c2, br1, br2;
    int fin1 = -1, fin2 = -1;

    fin1 = open(file1, O_RDONLY);
    fin2 = open(file2, O_RDONLY);

    if (fin1 == -1)
    {
        perror ("open");
        return -1;
    }

    if (fin2 == -1)
    {
        perror ("open");
        return -1;
    }

    br1 = read (fin1, &c1, 1);
    br2 = read (fin2, &c2, 1);

    while((br1 > 0) && (br2 > 0) && results)
    {
        br1 = read (fin1, &c1, 1);
        br2 = read (fin2, &c2, 1);
        results = (c1 == c2);

        printf("%c %c\n", c1, c2);
    }

    close(fin1);
    close(fin2);

    return results;
}


int main(int argc, char **argv)
{
    if(argc < 3)
    {
        printf("Example usage:\n\n\t %s file1 file2\n\n", argv[0]);
        exit(-1);
    }
    else
    {
        printf("%s %s\n", argv[1], argv[2]);

        if(CompareFileByteByByte(argv[1], argv[2]))
        {
            printf("Files are equal\n");
        }
        else
        {
            printf("Files are NOT equal\n");
        }
    }


    return 0;
}
8
  • 1
    did you try to compare file1 and a copy of file1? Commented Jun 11, 2016 at 8:20
  • @mch: Yes, I did - the result is the same as described here: it shows that those files are NOT equal, and the return value of the program is 255, instead of 0. Commented Jun 11, 2016 at 8:22
  • I'm not sure if your code is fit to compare a file with itself: before all, it is meant to work on two different files. Commented Jun 11, 2016 at 8:22
  • 1
    Should c1 and c2 by char and not int Commented Jun 11, 2016 at 8:27
  • 1
    Again a perfect opportunity to use a debugger. Do it, it's fun! :-) Commented Jun 11, 2016 at 8:28

1 Answer 1

4

You never enter to the loop because of results is 0

 while((br1 > 0) && (br2 > 0) && results)
Sign up to request clarification or add additional context in comments.

4 Comments

plus he not handle case when one file if prefix of another
After the first br2 = read... line, you need results = (br1 == br2) && (c1 == c2);. And you need a similar fix at the end as well (in case one file is longer than the other, you can't just stop comparing when one file ends).
Stil, Files are NOT equal and it returns 255
You have lots of bugs. At least four.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.