Witam. Muszę zrobić program który porównuje zawartość wszystkich plików w zadanym folderze i wykrywa duplikaty. Program działa dobrze do momentu porównywania plików w pętli for. Podejrzewam że może być coś nie tak z tablicą ale nie mogę znaleźć w niej błędu.
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
int listFiles(char *path, int a, char ***array);
int main()
{
char path[100];
char **array=NULL;
int a=0;
printf("Enter path to list files: ");
scanf("%s", path);
listFiles(path,a, &array);
return 0;
}
int listFiles(char *basePath, int a, char ***array)
{
char path[1000],c,*d=NULL;
FILE *input_file;
struct dirent *dp;
DIR *dir = opendir(basePath);
if (!dir){
return 1;
}
while ((dp = readdir(dir)) != NULL){
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0){
strcpy(path, basePath);
strcat(path, "\\");
strcat(path, dp->d_name);
input_file = fopen(path, "r");
if(input_file == NULL){
fclose(input_file);
}
if (input_file){
printf("%s\n",path);
while ((c = getc(input_file)) != EOF)
d=realloc(d, (x+1)*sizeof(*d));
(d)[x++] = c;
}
*array = realloc(*array, (a+1)*sizeof(**array));
(*array)[a++] = d;
if(a>1){
for(int j=0;j<a-1;++j){
if(!memcmp(array[a],array[j],999)){
printf("duplikat\n");
free(array[a]);
--a;
break;
}
}
}
}
listFiles(path,a,array);
}
closedir(dir);
return 0;
}