Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 comparing a file line by line
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
dchini




PostPosted: Sat Oct 18, 2008 5:51 pm   Post subject: 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


code:
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" );
        }
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Oct 18, 2008 6:26 pm   Post subject: 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




PostPosted: Tue Oct 21, 2008 5:19 pm   Post subject: RE:comparing a file line by line

This looks like a job for 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.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: