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!
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)
.