I'm writing a program that basically searches a directory and all its sub-directories for duplicate files. I have refined both the question and the code according to your suggestions (functions that needed to return a default value have been fixed to do so) so here it goes...
Here is the code of the comparing functions:
int compare()
{
int a, b;
unsigned char byte1, byte2;
while(1)
{
a = fread(&byte1, 1, 1, file1);
b = fread(&byte2, 1, 1, file2);
if(a == 0 && b == 0) break;
if(a != b) return 1;
if(byte2 != byte1) return 1;
}
return 0;
}
void startCompare()
{
char path1[1000], path2[1000];
FILE *reference = fopen("list.comp", "r");
FILE *other = fopen("list2.comp", "r");
int i, flag, j;
i = 0;
while(fgets(path1, 1000, reference))
{
flag = 0;
strtok(path1, "\n");
openFile1(path1);
for(j = 0; j <= i; ++j)
{
fgets(path2, 1000, other);
}
while(fgets(path2, 1000, other))
{
strtok(path2, "\n");
openFile2(path2);
if(!compare())
{
printf("Checking: %s vs. %s --> DUPLICATE\n", path1, path2);
flag = 1;
break;
}
else
{
printf("Checking: %s vs. %s --> DIFFERENT\n", path1, path2);
}
}
if(flag == 1)
{
printf("Will be deleted.\n");
}
}
}
(The startCompare() function is called first)
Now, the directory itself has these files:
- bloblo
- bloblo/frofo
- bloblo/frofo/New Folder
- bloblo/frofo/New Folder (2)
- bloblo/frofo/New Folder (2)/New Folder (3)
- bloblo/frofo/New Folder (2)/New Folder (3)/New Text Document.txt
- bloblo/frofo/New Folder (2)/New Folder (3)/Untitled4
- 0.comp
- 1.comp
- 2.comp
- 3.comp
- 4.comp
- 5.comp
- 11.comp
- 100.comp
- duplicate_delete.dev
- duplicate_delete.exe
- duplicate_delete.layout
- list.comp
- list2.comp
- main.c
- main.o
- Makefile.win
- Untitled5.c
- Untitled5.exe
The output is:
Checking: 0.comp vs. 1.comp --> DIFFERENT
Checking: 0.comp vs. 100.comp --> DIFFERENT
Checking: 0.comp vs. 11.comp --> DIFFERENT
Checking: 0.comp vs. 2.comp --> DIFFERENT
Checking: 0.comp vs. 3.comp --> DIFFERENT
Checking: 0.comp vs. 4.comp --> DIFFERENT
Checking: 0.comp vs. 5.comp --> DIFFERENT
Checking: 0.comp vs. duplicate_delete.dev --> DIFFERENT
Checking: 0.comp vs. duplicate_delete.exe --> DIFFERENT
Checking: 0.comp vs. duplicate_delete.layout --> DIFFERENT
Checking: 0.comp vs. list.comp --> DIFFERENT
Checking: 0.comp vs. list2.comp --> DIFFERENT
Checking: 0.comp vs. main.c --> DIFFERENT
Checking: 0.comp vs. main.o --> DIFFERENT
Checking: 0.comp vs. Makefile.win --> DIFFERENT
Checking: 0.comp vs. Untitled5.c --> DIFFERENT
Checking: 0.comp vs. Untitled5.exe --> DIFFERENT
With return code 0.
While what it should be printing is every file being checked with each other and finding that the files 100.comp and 11.comp are a duplicate of each other and the other files unique. So basically, why does it stop there? Why doesn't it continue to check? Is there any way this can be solved?