I am currently trying to write a program that reads two files one byte at a time (yes I am aware of the heavy I/O overhead), but I am having trouble incrementing the FILE pointer. I would like to program to compare both files byte-by-byte, and getc would not be a viable option for it would only work for chars because chars are one byte. However, I am reading from two text files and the text file could include numbers such as ints, doubles, etc. Therefore, in such scenario I would like to grab that byte from part of the int/double and compare it to the other file (a sequential byte-by-byte comparison).
Here is what I have so far:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <time.h>
#define BUFFER_SIZE 1
unsigned char buffer1[BUFFER_SIZE];
unsigned char buffer2[BUFFER_SIZE];
int main()
{
FILE *fp1, *fp2;
int ch1, ch2;
clock_t elapsed;
char fname1[40], fname2[40];
printf("Enter name of first file :");
fgets(fname1, 40, stdin);
while ( fname1[strlen(fname1) - 1] == '\n')
{
fname1[strlen(fname1) -1] = '\0';
}
printf("Enter name of second file:");
fgets(fname2, 40, stdin);
while ( fname2[strlen(fname2) - 1] == '\n')
{
fname2[strlen(fname2) -1] = '\0';
}
fp1 = fopen(fname1, "r");
if ( fp1 == NULL )
{
printf("Cannot open %s for reading\n", fname1 );
exit(1);
}
fp2 = fopen( fname2, "r");
if (fp2 == NULL)
{
printf("Cannot open %s for reading\n", fname2);
exit(1);
}
elapsed = clock(); // get starting time
/* Read in 256 8-bit numbers into the buffer */
size_t bytes_read1 = 0;
size_t bytes_read2 = 0;
bytes_read1 = fread(buffer1, sizeof(unsigned char), BUFFER_SIZE, fp1);
bytes_read2 = fread(buffer2, sizeof(unsigned char), BUFFER_SIZE, fp2);
printf("%c + in buffer 1\n", *buffer1);
printf("%c + in buffer 2\n", *buffer2);
fclose ( fp1 ); // close files
fclose ( fp2 );
elapsed = clock() - elapsed; // elapsed time
printf("That took %.4f seconds.\n", (float)elapsed/CLOCKS_PER_SEC);
return 0;
}
I am assuming buffer1 and buffer2 are the content of the one byte being read? Would I have to convert them to a number to compare them? I was thinking I could do the comparison as follows
(buffer1 ^ buffer2) == 0
Then that would mean they are equal based on the XOR bitwise operation
Thanks for your help in advance