Question

using these C libraries only #include <stdio.h> #include <sys/stat.h> #include <stdlib.h> #include <sys/time.h> (in C programing...

using these C libraries only

#include <stdio.h>

#include <sys/stat.h>

#include <stdlib.h>

#include <sys/time.h>

(in C programing language) How do you use file I/O to read in one byte at a time, as in reading straight from the hard disk with buffers turned off? And once you have access to one byte how would you compare each byte in one file to another file?

I understand how you would do that if you had a buffer to store chars and them compare each character. But how would you do that for individual bytes and without a buffer?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

void compare_two_binary_files(FILE *,FILE *);

int main(int argc, char *argv[])

{

FILE *fp1, *fp2;

if (argc < 3)

{

printf("\nInsufficient Arguments: \n");

printf("\nHelp:./executable <filename1> <filename2>\n");

return;

}

else

{

fp1 = fopen(argv[1], "r");

if (fp1 == NULL)

{

printf("\nError in opening file %s", argv[1]);

return;

}

fp2 = fopen(argv[2], "r");

if (fp2 == NULL)

{

printf("\nError in opening file %s", argv[2]);

return;

}

if ((fp1 != NULL) && (fp2 != NULL))

{

compare_two_binary_files(fp1, fp2);

}

}

}

/*

* compare two binary files character by character

*/

void compare_two_binary_files(FILE *fp1, FILE *fp2)

{

char ch1, ch2;

int flag = 0;

while (((ch1 = fgetc(fp1)) != EOF) &&((ch2 = fgetc(fp2)) != EOF))

{

/*

* character by character comparision

* if equal then continue by comparing till the end of files

*/

if (ch1 == ch2)

{

flag = 1;

continue;

}

/*

* If not equal then returns the byte position

*/

else

{

fseek(fp1, -1, SEEK_CUR);   

flag = 0;

break;

}

}

if (flag == 0)

{

printf("Two files are not equal : byte poistion at which two files differ is %d\n", ftell(fp1)+1);

}

else

{

printf("Two files are Equal\n ", ftell(fp1)+1);

}

}

Add a comment
Know the answer?
Add Answer to:
using these C libraries only #include <stdio.h> #include <sys/stat.h> #include <stdlib.h> #include <sys/time.h> (in C programing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT