sábado, 18 de julho de 2009

Verbose diff

Main feature/bug: Doesn't compare bytes at different positions. It is only useful to compare files in situations where you want to know if some byte was corrupted.

#include <stdio.h>
#include <assert.h>

int main(int argc, char ** argv){
FILE * f1;
FILE * f2;
unsigned long diff, tot;
assert(argc==3);
f1 = fopen(argv[1],"r");
f2 = fopen(argv[2],"r");
assert(f1);
assert(f2);
for(diff=0, tot=0; 1; tot++){
char c1 = getc(f1);
char c2 = getc(f2);
if(0==(tot%(1024*1024))){
printf("At %lu megabytes, %lu bytes differ.\n", tot/(1024*1024), diff);
fflush(stdout);
}
assert(!ferror(f1));
assert(!ferror(f2));
if(feof(f1)){
if(feof(f2)){
printf("Reached the end of both files. %lu of %lu bytes differ.\n", diff, tot);
if(diff==0){
return 0;
} else {
return 1;
}
} else {
printf("Reached the end of the first file. %lu of %lu bytes so far differ, but file two continues.\n", diff, tot);
return 1;
}
}
if(feof(f2)){
printf("Reached the end of the second file. %lu of %lu bytes so far differ, but file one continues.\n", diff, tot);
return 1;
}
if(c1!=c2){
diff++;
}
}
}

Sem comentários:

Enviar um comentário