473,287 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Comparing Two Files line by line and word by word

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

Jan 11 '06 #1
8 7482
Frost <ra*******@dgipro.com> wrote:
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, (code trimmed)


I don't have any comments on your code yet other than that the
formatting was atrocious. I know posting well-formatted code to
Usenet with Google's interface is a huge chore, but commenting on code
that looks that bad is not worth the effort for your average
comp.lang.c. guru. I've taken the liberty of reformatting it, which
may be enough to get you some useful feedback:

#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 ) {
i=strcmp( s1, s2 );
if( i == 0 ) {
count3++;
}
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( "both files have ended\n" );
}

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jan 11 '06 #2
thamks for ur help next time ill make sure code is formated properly

Jan 12 '06 #3
Frost wrote:
thamks for ur help next time ill make sure code is formated properly

^^ ^^^

Ignoring typos, those words are probably "your" and "I'll". Proper
spelling and capitalization makes things much easier to read. Also
read (and heed) the instructions and references below, to include
adequate context.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Jan 12 '06 #4

Christopher Benson-Manica wrote:
Frost <ra*******@dgipro.com> wrote:
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,and prints only dissimilar lines please can u give me a program which i can check word my word so that if the lines contain the same words but in different positions they should match.


#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 ) {
i=strcmp( s1, s2 );
if( i == 0 ) {
count3++;
}
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( "both files have ended\n" );
}


Jan 13 '06 #5
Frost wrote:
Christopher Benson-Manica wrote:
Frost <ra*******@dgipro.com> wrote:
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,and prints only dissimilar lines please can u give me a program which i can check word my word so that if the lines contain the same words but in different positions they should match.

I could, but I will not. At least not directly.
Let us first clean up your program. Then we will discuss what to do
to make it more efficient. The extension of what we do line by line
can be used for the word by word version.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 10000

main( int argc, char *argv[] )
main returns int. Do not rely on implicit int as
C99 made it illegal.
Write
int main (int argc, char **argv)
or
int main (void)
instead.
{
FILE *fp1, *fp2, *fp3;
void filecomp( FILE *, FILE * );
void fileconv( FILE *, FILE * );
This can bite you later on if you move the functionality
out of main(), especially if you rely on implicit int elsewhere
and do not turn up the warning level of your compiler...
if( argc != 4 ) {
fprintf( stderr, "need two files\n" );
exit( 1 );
Non-portable argument to exit(): Use EXIT_FAILURE instead.
To signal success, use EXIT_SUCCESS or 0.
}
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 );
}
Instead of closing and reopening, just open fp3 as "w+"
and rewind() it.
filecomp( fp1, fp3 );
fclose( fp1 );
fclose( fp3 );
exit( 0 );
Matter of taste:
return 0;
does not do exactly the same thing, but is appropriate
as well.}

void fileconv( FILE *fp2, FILE *fp3 )
{
char c;
The return type of fgetc() is int.
EOF is a negative int value.
char may be an unsigned type.

Moreover, fgetc() returns the character value converted
to unsigned char or EOF.
In short:
Make c an int.
while( (c=fgetc(fp2)) != EOF ) {
Note: The macro getc() does the same but may be implemented
more efficiently.
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;
Look at these names and tell me you will know what they mean
in twelve months' time. Keep a straight face.
while( ((s1=fgets(line1,MAX,fp1)) != NULL) ) {
count1++;
while( (s2=fgets(line2,MAX,fp3)) != NULL ) {
i=strcmp( s1, s2 );
if( i == 0 ) {
count3++;
}
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( "both files have ended\n" );
}


What is wrong with your approach: You ignore the possibility
that your line length might exceed MAX. Unfortunately, there
is no nice standard C way to do it right but you can use the
public domain ggets()/fggets() of C.B. Falconer which is portable.

How can that make problems?
Suppose l1 is too long and can be decomposed into l1a and l1b;
If we now have the fragments l1a and l1b somewhere in the second
file, we could be lead to the conclusion that the files are
identical even if they are not.

In addition, you will not find lines which are only in the
second file but not in the first one.

Let us consider a line by line comparison without multiple lines
and the points I raised above:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* Use the PD fggets() from http://cbfalconer.home.att.net/download/ */
#include "ggets/ggets.h"

void fileconv (FILE *conv_src, FILE *conv_dest);
void filecomp (FILE *comp_src, FILE *conv_dest);

int main (int argc, char **argv)
{
FILE *comp_src, *conv_src, *conv_dest;

if (argc != 4) {
fprintf(stderr, "Need THREE files (comp_src conv_src conv_dest)\n");
exit(EXIT_FAILURE);
}

/* Conversion */
conv_dest = fopen(argv[3], "w+");
conv_src = fopen(argv[2], "r");
if (!conv_src || !conv_dest) {
fprintf(stderr, "Cannot open files for conversion\n" );
(void)fclose(conv_dest);
(void)fclose(conv_src);
exit(EXIT_FAILURE);
}
fileconv(conv_src, conv_dest);
(void)fclose(conv_src);

/* Comparison */
comp_src = fopen(argv[1], "r");
if (comp_src == NULL) {
fprintf(stderr, "Cannot open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
rewind(conv_dest);
filecomp(comp_src, conv_dest);
(void)fclose(comp_src);
(void)fclose(conv_dest);

return 0;
}

void fileconv (FILE *conv_src, FILE *conv_dest)
{
int c;
while ((c = getc(conv_src)) != EOF) {
if (c == '.') {
c = '/';
}

if (EOF == putc(c, conv_dest )) {
/* Handle error and return */
}
}
}

void filecomp (FILE *src1, FILE *src2)
{
char *line1, *line2;
size_t line_count = 0;

rewind(src1); /* Does not hurt but makes sure that */
rewind(src2); /* we start from the beginning. */

while (0 == fggets(&line1, src1)) {
int is_equal=0;
++line_count;
if (0 == fggets(&line2, src2)) {
if (!strcmp(line1, line2))
is_equal = 1;
}
else
break;

free(line1); /* Drawback of *ggets(): You have to free() */
free(line2); /* yourself. */

if (!is_equal)
printf("line %lu of comp_src does not match "
"normalized lines of conv_src\n",
(unsigned long)line_count);
}
if (EOF == fggets(&line1, src1)
&& EOF == fggets(&line2, src2))
printf("both files have ended\n");

free(line1);
free(line2);
}

Now, how can we give that at least the functionality you had?
As I understand, you cared nothing about the position of the
lines within the files.
So: Read in all lines of src1, store them in an array a1 of
strings; do the same for src2 (->array a2).
Sort them, e.g. using strcmp() and qsort().
If you are not interested in multiples, you can throw them
out at sorting if you sort yourself or do it after sorting.
Otherwise, go to the last of the multiple lines when
going through the lines.

In the following, I pretend there are no multiple lines:
Now, proceed through the arrays and check all lines whether
they are identical.
BTW: You do not even to generate a separate converted file;
when reading in the lines, do your "conversion" on the fly.

What have we lost? The line numbers. Okay, let us use arrays
a1, a2 of struct line { size_t line_no; char *line_content;}
instead and pass the difference.

Now, the last step: Before sorting the lines, sort every line's
words. This can be, for example, done by splitting the line into
an array of strings where every string is a word, sorting this
array, and generating a string from it containing all the words,
separated by a space.

Just try it; the clc crowd will help you if you struggle with
the details.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jan 14 '06 #6
thanks for ur help u been very helpful,ur program is awsome but i am
not getting the idea of what u asked me to do or how to do it
i have basic knowledge of c and now im learning all these file
handling,datastuctures,pointers in c so can u help me here agian if it
is not to much trouble.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Now, how can we give that at least the functionality you had?
As I understand, you cared nothing about the position of the
lines within the files.
So: Read in all lines of src1, store them in an array a1 of
strings; do the same for src2 (->array a2).
Sort them, e.g. using strcmp() and qsort().
If you are not interested in multiples, you can throw them
out at sorting if you sort yourself or do it after sorting.
Otherwise, go to the last of the multiple lines when
going through the lines.
Now, the last step: Before sorting the lines, sort every line's
words. This can be, for example, done by splitting the line into
an array of strings where every string is a word, sorting this
array, and generating a string from it containing all the words,
separated by a space
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Jan 17 '06 #7
hello all thanks for all the replies but im still stuck on line by line
word comparison part between two files, the lines may contain the same
words in different places what i mean to say is contents of both the
lines are same hence they should match can some one help me with such a
program.

Feb 10 '06 #8
Frost wrote:
hello all thanks for all the replies but im still stuck on line by line
word comparison part between two files, the lines may contain the same
words in different places what i mean to say is contents of both the
lines are same hence they should match can some one help me with such a
program.


I guess you want to say that these two lines are the same:

foo bar baz
bar foo baz

One solution would be to create a sorted list of words in each of the
two lines you're comparing. Then test whether two lists are equivalent.
You may also have to decide whether case matters, and what constitutes
a word.

Possible reason for your previous post being ignored is that no-one
here will give you lectures in basics that you can get from good
textbooks. Look up all concepts and functions mentioned, study, try to
use. If you encounter problems in the /last/ bit, come back here for
help.

In terms of general algorithms and programming methods, comp.programmer
is a much better place to ask. It's not frowned upon here, either, but
direct C-related questions are ofprimary interest.

Also, please read (and heed) this, again:

"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>

--
BR, Vladimir

Feb 10 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: MM | last post by:
Hi I have never written any C programs before, but it seems that I need to do so now. Hope some of you out there can spend a few minutes and help me by writing a simple example of something...
4
by: ddd | last post by:
I am trying to build a diff tool that allows me to compare two HTML files. I am looking for resources on how to achive this. The main problem is that I do not want to simply highlight the line of...
0
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add...
9
by: Phoe6 | last post by:
Hi, Following are my files. In the format: Filename ---- content ---- config1.txt
18
by: Carramba | last post by:
Hi! is there a better/faster way to compare mantissas of to real number then in following code? #include <stdio.h> #include <stdlib.h> int main(void) { float a,b; int test;
4
by: andrewwan1980 | last post by:
I've got two websites, one original, the other based off the original. I like to diff/compare the websites using diff automatic comparison tools to see what text/information has changed. The...
5
by: alivip | last post by:
How can I get every Token (word) and PreviousToken(Previous word) From multube files and frequency of each two word my code is trying to get all single word and double word (every Token (word) and...
1
by: Avi1 | last post by:
Hi, I got the code (from the internet)for comparing two files and showing the difference in contents.Now,I tried the same code for two files written in japanese language(kanji).If I save the two...
9
Thekid
by: Thekid | last post by:
Hi, I can't seem to figure this out. Here's my objective: I have a value that is an md5 hash and I have a wordlist. I need to md5 the words in the list, then compare them to the given hash, then have...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.