Skip to content

Instantly share code, notes, and snippets.

@DreamVB
Created August 11, 2016 21:28
Show Gist options
  • Select an option

  • Save DreamVB/b69a94002e10cebfa682adadc98cf103 to your computer and use it in GitHub Desktop.

Select an option

Save DreamVB/b69a94002e10cebfa682adadc98cf103 to your computer and use it in GitHub Desktop.
Simple tool to compare the contents of two files.
/*
fCompare By DreamVB
Version 1.0
Tool to compare the contents of two files.
Please feel free to to use as you like.
Please let me know if you found this code usfull e. dreamvb@outlook.com
*/
#include <stdio.h>
#include <string>
int main(int argc, char* argv[]){
FILE *f1 = NULL;
FILE *f2 = NULL;
int addr = 0;
int Ret = 2;
char c = '\0';
char d = '\0';
if(argc < 3){
printf("Usage: <InFile> <OutFile>\n");
return 0;
}
//Open file 1
f1 = fopen(argv[1],"r");
//Check for errord.
if(f1 == NULL){
printf("IO/Error cannot open first file.\n");
return 0;
}
//Open file 2
f2 = fopen(argv[2],"r");
//Check for error
if(!f2){
printf("IO/Error cannot open second file.\n");
return 0;
}
//Get a char from each file.
c = getc(f1); //File1
d = getc(f2); //File2
//First check to see if we have a match on the two chars c & d
if(c == d){
//Test that we are not at the end of the files and that both c & d match
while((c != EOF) && (d != EOF) && (c == d)){
//Fetch next chars
c = getc(f1);
d = getc(f2);
//Current index that we are at
addr++;
}
}
//Looks like the files are a match
if(c == d){
printf("Both files are the same.\n");
}else{
//Looks like we have a bad match.
Ret = 1;
printf("Files do not match, Bad char at address: %d\n\n",addr);
printf("Char at address %d in first file is %c\n",addr,c);
printf("Char at address %d in second file is %c\n",addr,d);
}
//Close files
fclose(f1);
fclose(f2);
return Ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment