0

I would like to take two files, compare them byte-by-byte, and test it's performance,

So far, this is what my code looks like:

#include<stdio.h>
#include <time.h>

int main()
{
    FILE *fp1, *fp2;
    int ch1, ch2;
    char fname1[40], fname2[40] ;

    printf("Enter name of first file :") ;
    gets(fname1);

    printf("Enter name of second file:");
    gets(fname2);

    clock();    

    fp1 = fopen( fname1,  "r" );
    fp2 = fopen( fname2,  "r" ) ;

    if ( fp1 == NULL )
    {
        printf("Cannot open %s for reading\n", fname1 );
        exit(1);
    }
    else if (fp2 == NULL)
    {
        printf("Cannot open %s for reading\n", fname2 );
        exit(1);
    }
    else
    {
        ch1  =  getc( fp1 ) ;
        ch2  =  getc( fp2 ) ;

        while( (ch1!=EOF) && (ch2!=EOF) && (ch1 == ch2))
        {
            ch1 = getc(fp1);
            ch2 = getc(fp2);
        }

        if (ch1 == ch2)
            printf("Files are identical\n");
        else if (ch1 != ch2)
            printf("Files are Not identical\n");

        fclose ( fp1 );
        fclose ( fp2 );
    }

    printf("That took %d seconds.\n", clock() / CLOCKS_PER_SEC);

    return 0;
}

This code compares two files if they are identical, but I would like to check by using the bitwise operation of XOR, any ideas of how I can do this?

Is there any way I can read in a file one byte at a time?

Thanks for your help in advance!

humblebeast
  • 303
  • 3
  • 16
  • What do you mean by *"but I would like to check if one file itself have duplication"*? – Matthew Jul 18 '14 at 21:14
  • I have an idea, is there a way I can read in a file one byte at a time? I'm sorry, that is a typo, all I would like is to compare two files byte-by-byte using XOR – humblebeast Jul 18 '14 at 21:19
  • If two bytes that are identical, then the result should be zero in the end after performing an XOR bitwise operation, but I am having trouble implementing this – humblebeast Jul 18 '14 at 21:21
  • 1
    Do you really want to print: "Files are identical\n" every time a byte matches? i.e. if you have a 4 KB file you print this message 4096 times. I think the answer is no. Also, if you truly are interested in performance then you are going to spend all your time in the print, not in the actual comparison of the files. You will want to put your result in a bool and print it at the end of the function. Also, it's probably worth mention that the compiler is free to use XOR for the equality check if it wants to. Your best bet is usually to write code so that your intentions are clear to the reader. – Apriori Jul 18 '14 at 23:10
  • @Apriori you are correct, I don't want to print each time there are identical bytes. However, the getc option will not work for what I am trying to accomplish. I just noticed that. For example, it will work for a char because a char is one byte, but what if I was reading a file with numbers? It would not longer be one byte. Any ideas on how I can take in a number (such as an int), break it down into it's lowest possible data type in C? Thanks – humblebeast Jul 19 '14 at 17:32
  • @humblebeast: you're interested in byte by byte equality of the file, correct? It does not matter if it contains ints, newlines, or anything else. Everything gets read as a character untill the end of the file. Now that said, if you're interested in a byte by byte comparison of the files then you should probably be opening them as binary files, not text files, and reading them byte by bye that way. Does that make sense? Let me know if you need a hand figuring how to do any of that. (Writing this on my phone) – Apriori Jul 19 '14 at 19:19

1 Answers1

1

How about taking your code exactly as it is and changing this in your main loop?

 while( (ch1!=EOF) && (ch2!=EOF) && !(ch1 ^ ch2))
 {
    ch1 = getc(fp1);
    ch2 = getc(fp2);
 }

Quick note: You're already reading the file byte by byte. getc() is reading only a character from the file but you then store it as an int. From getc() function's description: On success, the character read is returned (promoted to an int value).

dragosht
  • 3,237
  • 2
  • 23
  • 32