I'm writing a project that has the task of calculating the Edit distance between two input files. The goal is to compare any type of file. The problem comes when I go to run and this is the result.
OUTPUT
filedistance distance pdf1.pdf pdf2.pdf
EDIT DISTANCE: 216
filedistance distance sample.jpg sample2.jpg
EDIT DISTANCE: 0
filedistance distance file1.txt file2.txt EDIT DISTANCE: 11
With a .txt file or a .pdf file it works fine. It doesn't work with a file other than these. Obviously the compared files are different, so it is impossible that b.jpg has a distance from c.jpg equal to 0
This is my solution, but I don't see any problem.
I'm going to read the files as binary files, in this way I shouldn't have any problem for any extension.
int levenshtein_distance(char *file1, char *file2){
char *str1 = getString(file1);
char *str2 = getString(file2);
int n = (int)strlen(str1) + 1;
int m = (int)strlen(str2) + 1;
if (n == 0)
return m;
if (m == 0)
return n;
int **matrix = matGenerate(str1, n, str2, m);
int distance = matrix[n - 1][m - 1];
deallocateMat(n, matrix);
free(str1);
free(str2);
return distance;
}
char *getString(char *file){
FILE *inputFile = fopen(file, "rb");
if (inputFile == NULL){
perror("Could not open file");
exit(1);
}
fseek(inputFile, 0L, SEEK_END);
long sizeFile = ftell(inputFile);
rewind(inputFile);
char *string = calloc(1, sizeFile);
if (string != NULL){
fread(string, sizeFile, 1, inputFile);
}
fclose(inputFile);
return string;
}
Methods like matGenerate and deallocateMat are used to fill and empty the array. If requested, I can attach them.
I can't understand why it doesn't work with files other than pdf and txt. Can you help me?