#include <stdio.h>
#include <stdlib.h>
#define null (0)
int main(int argc, char** argv)
{
if (argc != 3)
{
printf("Usage: compare-files.exe FILENAME1 FILENAME2\n");
exit(EXIT_FAILURE);
}
const char* FILENAME1 = argv[1];
const char* FILENAME2 = argv[2];
bool error1 = false;
bool error2 = false;
FILE* f1 = fopen(FILENAME1, "rb");
if (f1 == null)
{
error1 = true;
}
FILE* f2 = fopen(FILENAME2, "rb");
if (f2 == null)
{
error2 = true;
}
if (error1 && error2)
{
printf("RESULT=NO Error opening files \"%s\" and \"%s\" for reading\n", FILENAME1, FILENAME2);
exit(EXIT_FAILURE);
}
if (error1)
{
printf("RESULT=NO Error opening file \"%s\" for reading\n", FILENAME1);
exit(EXIT_FAILURE);
}
if (error2)
{
printf("RESULT=NO Error opening file \"%s\" for reading\n", FILENAME2);
exit(EXIT_FAILURE);
}
int count = 0;
for (;;)
{
int ch1 = fgetc(f1);
int ch2 = fgetc(f2);
count++;
if ((ch1 == EOF) && (ch2 == EOF))
{
printf("RESULT=YES\n");
exit(EXIT_SUCCESS);
}
else if ((ch1 == EOF) || (ch2 == EOF))
{
printf("RESULT=NO Files are of different lengths at position %d\n", count);
exit(EXIT_FAILURE);
}
else if (ch1 != ch2)
{
printf("RESULT=NO Files different at position %d\n", count);
exit(EXIT_FAILURE);
}
}
}