Program for comparing records of two files passed as argument parameter.

#include<stdio.h>
#include<stdlib.h>
void strparse(char *buf, char *[]);
void compare(char *,char *);

int main(int argc,char *argv[])
{
compare(argv[1],argv[2]);
}
void  strparse(char *buf,char *str[])    /*string that is passed is parsed with respect to |. */
{
int i=0,j=0;
char *tempb;
for(tempb=buf; *tempb!=''; tempb++)
{
if(!str[j])
str[j]=(char*)malloc(90);

if(*tempb!='|')
{
*(str[j]+i)=*tempb;
i++;
}
else
{
*(str[j]+i)='';
j++;
i=0;
}
}
}

void compare(char *f1, char *f2)
{
int eno,eage, eno1,eage1;
char enm[26],enm1[26],buf[80],buf1[80],*str1[80] = {0,}, *str2[80] = {0,};
double esal=0.0,esal1=0.0;
FILE *fp1,*fp2;
fp1=fopen(f1,"r");
fp2=fopen(f2,"r");

if(!fp1 || !fp2)
printf(" Error in file open \n");

fgets(buf,80,fp1);
fgets(buf1,80,fp2);

strparse(buf,str1);
eno=atoi(str1[0]);
strcpy(enm,str1[1]);
eage=atoi(str1[2]);
esal=atof(str1[3]);

strparse(buf1,str2);
eno1=atoi(str2[0]);
strcpy(enm1,str2[1]);
eage1=atoi(str2[2]);
esal1=atof(str2[3]);

while(!(feof(fp1) || feof(fp2)))
{
if(eno==eno1)
{

if(!(!strcmp(enm,enm1))&&(eage1==eage)&&(esal==esal1))  /*Print the record  from 1st file,
if eno in both
files are same but, fields are different. */
printf("%s",buf);
fgets(buf,80,fp1);
fgets(buf1,80,fp2);
strparse(buf1,str2);
eno1=atoi(str2[0]);
strcpy(enm1,str2[1]);
eage1=atoi(str2[2]);
esal1=atof(str2[3]);
strparse(buf,str1);
eno=atoi(str1[0]);
strcpy(enm,str1[1]);
eage=atoi(str1[2]);
esal=atof(str1[3]);
}
else
{
if(eno>eno1)
{
fgets(buf1,80,fp2);
strparse(buf1,str2);
eno1=atoi(str2[0]);
strcpy(enm1,str2[1]);
eage1=atoi(str2[2]);
esal1=atof(str2[3]);
}
else
{
printf("%s",buf);    /*Print record from 1st file having eno not present in another file */
fgets(buf,80,fp1);
strparse(buf,str1);
eno=atoi(str1[0]);
strcpy(enm,str1[1]);
eage=atoi(str1[2]);
esal=atof(str1[3]);
}
}
}
fclose(fp1);
fclose(fp2);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s