Latest News

Similarity Test

Sunday, 27 June 2010 , Posted by genesisdatabase at 03:44

Ever wanted to compare and contrast 2 file to see how much difference they have in terms of each byte?  Now here's a project i called "Similarity" under my precious folder called "rubbish".  Originally i wanted to talk about my project that is called "Bejeweled Clicker" however i am lazy right now as i just got up so i'll release some simple stuffs instead =)

[code]
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
char Filename[256];
int Filesize;
}File;

int MinSize = 0;
int MaxSize = 0;
int Similarity = 0;
int Dissimilarity = 0;
int GetFileSize2(char *Filename)
{
int fsize = 0;
FILE *Read = NULL;

Read = fopen(Filename, "rb");
if(!Read)
return 0;

fseek(Read, 0, SEEK_END);
fsize = ftell(Read);
rewind(Read);
fclose(Read);

return fsize;
}
int main()
{
File *f1, *f2;
f1 = (File *)malloc(sizeof(File));
f2 = (File *)malloc(sizeof(File));

// 1, get filenames
printf("Enter file 1: ");
fflush(stdin);
scanf("%[^\n]", f1->Filename);

printf("Enter file 2: ");
fflush(stdin);
scanf("%[^\n]", f2->Filename);

// 2, get filesizes
f1->Filesize = GetFileSize2(f1->Filename);
f2->Filesize = GetFileSize2(f2->Filename);
if(!f1->Filesize || !f2->Filesize)
{
printf("\n");
printf("One of the filename doesn't exist or does not contain any data\n");
fflush(stdin);
getchar();
return 0;
}

printf("\n");
printf("Begin comparison\n");
printf("File 1 - %s : %d bytes\n", f1->Filename, f1->Filesize);
printf("File 2 - %s : %d bytes\n", f2->Filename, f2->Filesize);

if(f1->Filesize > f2->Filesize)
{
printf("\n");
printf("1. Filesize difference : %d\n", f1->Filesize - f2->Filesize);
MinSize = f2->Filesize;
MaxSize = f1->Filesize;
}
else if(f1->Filesize < f2->Filesize)
{
printf("\n");
printf("1. Filesize difference : %d\n", f2->Filesize - f1->Filesize);
MinSize = f1->Filesize;
MaxSize = f2->Filesize;
}
else
{
printf("\n");
printf("1. Filesize difference : %d\n", 0);
MinSize = f1->Filesize | f2->Filesize;
MaxSize = f1->Filesize | f2->Filesize;
}

FILE *open1 = fopen(f1->Filename, "rb");
FILE *open2 = fopen(f2->Filename, "rb");
if(!open1 || !open2)
{
printf("\n");
printf("Error opening one of the files\n");
fflush(stdin);
getchar();
return 0;
}

char *buf1 = (char *)malloc(f1->Filesize);
char *buf2 = (char *)malloc(f2->Filesize);
if(!buf1 || !buf2)
{
printf("\n");
printf("Error allocating buffer\n");
fflush(stdin);
getchar();
return 0;
}

fread(buf1, sizeof(char), f1->Filesize, open1);
fread(buf2, sizeof(char), f2->Filesize, open2);

for(int i = 0 ; i < MinSize ; i++)
{
if(buf1[i] != buf2[i])
Dissimilarity++;
else
Similarity++;
}

printf("2. Similarity in percent: %0.2f\n", (float)Similarity/MinSize * 100);
printf(" Dissimilarity in percent: %0.2f", (float)Dissimilarity/MinSize * 100);

fflush(stdin);
getchar();
return 1;
}
[/code]

I'd like to point out that i am used to return 1; at the end of main function. However do realize that return 0; is infact the correct return value. I sort of use return 1; as return success of function in all cases.

What the codes do is that it first checks the filesize to see whether they are equal or otherwise.  Later it reads every byte of the both file into a char pointer.  I then used a for loop to compare each bytes of the char pointer.

This source code is more to beginners learning how to get used to the FILE structure and some memory allocation (malloc).

Currently have 0 comments:

Leave a Reply

Post a Comment