F
Frost
Hi All,
I am a newbie i have written a c program on unix for line by line
comparison for two files now could some one help on how i could do word
by word comparison in case both lines have the same words but in
jumbled order they should match and print only the dissimilar lines.The
program also checks for multiple entries of the same line.
Here file 2 converts to file 3 which is in the format of file1 and i
compare file1 with file3.
this program right now is checking line by line and for multiple
entries of the same line,
the program i wrote for word to word comparison is included in the
comments which is not working if some could fix i would really be
relieved.
---------------------------------------------------------------------------------------------------------------------------------------
//==================================================================
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000
//==================================================================
main(int argc,char *argv[])
{
FILE *fp1,*fp2,*fp3;
void filecomp(FILE *,FILE *);
void fileconv(FILE *,FILE *);
if(argc!=4)
{
fprintf(stderr,"need two files\n");
exit(1);
}
fp3=fopen(argv[3],"w");
fp2=fopen(argv[2],"r");
if(fp2==NULL)
{
printf("cannot open file2\n");
exit(1);
}
if (fp3==NULL)
{
printf("cannot open file3\n");
exit(1);
}
fileconv(fp2,fp3);
fclose(fp2);
fclose(fp3);
fp1=fopen(argv[1],"r");
if(fp1==NULL)
{
printf("cannot open file1\n");
exit(1);
}
fp3=fopen(argv[3],"r");
if(fp3==NULL)
{
printf("cannot open file3\n");
exit(1);
}
filecomp(fp1,fp3);
fclose(fp1);
fclose(fp3);
exit(0);
}
//==================================================================
void fileconv(FILE *fp2,FILE *fp3)
{
char c;
while((c=fgetc(fp2))!=EOF)
{
if(c=='.')
{
c='/';
fputc(c,fp3);
}
else
fputc(c,fp3);
}
}
//==================================================================
void filecomp(FILE *fp1,FILE *fp3)
{
char line1[MAX],line2[MAX];
char *s1,*s2;
int ctr,octr,a=0,b=1;
int i,count1=0,count2=1,count3=0,count4=0;
while(((s1=fgets(line1,MAX,fp1))!=NULL))
{
count1++;
while((s2=fgets(line2,MAX,fp3))!=NULL)
{
/* while (ctr!=EOF)
{
ctr=fgetc(fp1);
while(octr!=EOF)
{
octr=fgetc(fp3);
while(octr!='\n')
{
a++;
if(ctr==octr)
{
fseek(fp1,-a,1);
ctr=fgetc(fp1);
octr=fgetc(fp3);
b++;
}
while(ctr=='\n')
{
a=0;
ctr=fgetc(fp1);
}
}
b=0;
}
}*/
i=strcmp(s1,s2);
if(i==0)
{
count3++;
}
else
{
// printf("line %d of file1 is not equal to line %d of
file3\n\n",count2,count1);
}
count2++;
}
if(count3==0)
printf("line %d of file1 does not match lines of
file3\n",count1);
count2 = 1;
count3 = 0;
fseek(fp3,0,SEEK_SET);
}
if((s2=fgets(line2,MAX,fp3))!=NULL)
//printf("file1 has ended but not file3\n");
//else
printf("both files have ended\n");
}
---------------------------------------------------------------------------------------------------------------------------------------thanking
u,
Frost
I am a newbie i have written a c program on unix for line by line
comparison for two files now could some one help on how i could do word
by word comparison in case both lines have the same words but in
jumbled order they should match and print only the dissimilar lines.The
program also checks for multiple entries of the same line.
Here file 2 converts to file 3 which is in the format of file1 and i
compare file1 with file3.
this program right now is checking line by line and for multiple
entries of the same line,
the program i wrote for word to word comparison is included in the
comments which is not working if some could fix i would really be
relieved.
---------------------------------------------------------------------------------------------------------------------------------------
//==================================================================
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000
//==================================================================
main(int argc,char *argv[])
{
FILE *fp1,*fp2,*fp3;
void filecomp(FILE *,FILE *);
void fileconv(FILE *,FILE *);
if(argc!=4)
{
fprintf(stderr,"need two files\n");
exit(1);
}
fp3=fopen(argv[3],"w");
fp2=fopen(argv[2],"r");
if(fp2==NULL)
{
printf("cannot open file2\n");
exit(1);
}
if (fp3==NULL)
{
printf("cannot open file3\n");
exit(1);
}
fileconv(fp2,fp3);
fclose(fp2);
fclose(fp3);
fp1=fopen(argv[1],"r");
if(fp1==NULL)
{
printf("cannot open file1\n");
exit(1);
}
fp3=fopen(argv[3],"r");
if(fp3==NULL)
{
printf("cannot open file3\n");
exit(1);
}
filecomp(fp1,fp3);
fclose(fp1);
fclose(fp3);
exit(0);
}
//==================================================================
void fileconv(FILE *fp2,FILE *fp3)
{
char c;
while((c=fgetc(fp2))!=EOF)
{
if(c=='.')
{
c='/';
fputc(c,fp3);
}
else
fputc(c,fp3);
}
}
//==================================================================
void filecomp(FILE *fp1,FILE *fp3)
{
char line1[MAX],line2[MAX];
char *s1,*s2;
int ctr,octr,a=0,b=1;
int i,count1=0,count2=1,count3=0,count4=0;
while(((s1=fgets(line1,MAX,fp1))!=NULL))
{
count1++;
while((s2=fgets(line2,MAX,fp3))!=NULL)
{
/* while (ctr!=EOF)
{
ctr=fgetc(fp1);
while(octr!=EOF)
{
octr=fgetc(fp3);
while(octr!='\n')
{
a++;
if(ctr==octr)
{
fseek(fp1,-a,1);
ctr=fgetc(fp1);
octr=fgetc(fp3);
b++;
}
while(ctr=='\n')
{
a=0;
ctr=fgetc(fp1);
}
}
b=0;
}
}*/
i=strcmp(s1,s2);
if(i==0)
{
count3++;
}
else
{
// printf("line %d of file1 is not equal to line %d of
file3\n\n",count2,count1);
}
count2++;
}
if(count3==0)
printf("line %d of file1 does not match lines of
file3\n",count1);
count2 = 1;
count3 = 0;
fseek(fp3,0,SEEK_SET);
}
if((s2=fgets(line2,MAX,fp3))!=NULL)
//printf("file1 has ended but not file3\n");
//else
printf("both files have ended\n");
}
---------------------------------------------------------------------------------------------------------------------------------------thanking
u,
Frost