
-----------------------------------
dchini
Sat Oct 18, 2008 5:51 pm

comparing a file line by line
-----------------------------------
Alright the code i have right now compares two files line by line.

What i want my code to do is to make sure the words on the line are the same (it doesnt matter how many spaces there are)
The code below checks char for char i believe..... any help checking word for word or how to get this line for line to work would be extremely helpful


void filecomp(char f1[], char f2[])
	{
		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;

		if((fp1 = fopen(f1, "rb")) == 0)
		{
			perror("fopen1");
		}

		if((fp3 = fopen(f2, "rb")) == 0)
		{
			perror("fopen2");
			exit(1);
		}

        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" );
        }

-----------------------------------
wtd
Sat Oct 18, 2008 6:26 pm

RE:comparing a file line by line
-----------------------------------
I would like to suggest moving some stuff out into separate functions.  For instance, you need to compare words ignoring whitespace.  Try creating a "normalize_whitespace" function which takes in a string and returns a new string with extraneous whitespace removed.  "Hello,   world   " would become "Hello, world".

-----------------------------------
Pockets
Tue Oct 21, 2008 5:19 pm

RE:comparing a file line by line
-----------------------------------
This looks like a job for [url=http://linux.die.net/man/3/strtok]strtok()! Just remember to use ' ' as your delimiter. Strtok will take continguous delimiters (eg: two or more spaces) as a single delimiter, so it does most of the work for you. Don't forget that when it's done tokenizing, it returns NULL.
