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

Username:   Password: 
 RegisterRegister   
 word for word comparison
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
dchini




PostPosted: Thu Oct 23, 2008 10:28 pm   Post subject: word for word comparison

i have this code to compare two files word for word... my code is having problems and i cant figure it out..... if anyone has any ideas id be very happy!

code:

int wordForWord(FILE *fp1, FILE *fp2)
{
        char firstWord[10];
        char secondWord[10];
        int numberOfFirst = 0;
        int numberOfSecond = 0;

        while(1)
        {
                if(!fscanf(fp1, "%s", firstWord))
                        return -1;
                if(!fscanf(fp2, "%s", secondWord))
                        return -2;
                numberOfFirst++;
                numberOfSecond++;

                if(feof(fp1) && !feof(fp2))
                {
                                if(strcmp(firstWord, secondWord) < 0)
                                {
                                        printf("EOF on first file\n");
                                        return CMP_EOF_FIRST;
                                }
                }
                if(feof(fp1))
                {
                        if(!feof(fp2))
                        {
                                        printf("EOF on first file\n");
                                        return CMP_EOF_FIRST;
                        }
                }
                if(feof(fp1))
                {
                        if(!feof(fp2))
                        {
                                if(strcmp(firstWord, secondWord) > 0)
                                {
                                        printf("EOF on second file\n");
                                        return CMP_EOF_SECOND;
                                }
                        }
                }
                if(feof(fp2))
                {
                        if(!feof(fp1))
                        {
                                        printf("EOF on second file\n");
                                        return CMP_EOF_SECOND;
                        }
                }

                if(feof(fp1))
                {
                        if(feof(fp2))
                        {
                                if(strcmp(firstWord, secondWord) == 0)
                                {
                                        printf("files are equal\n");
                                        return CMP_EQUAL;
                                }
                        }
                }

                if(strcmp(firstWord, secondWord) != 0)
                {
                        printf("These files differ at word %d\n", numberOfFirst);
                        return -3;
                }

        }
}
Sponsor
Sponsor
Sponsor
sponsor
dchini




PostPosted: Thu Oct 23, 2008 10:30 pm   Post subject: RE:word for word comparison

in a test that i had run it had printed "EOF on file one" when is should have been equal
dchini




PostPosted: Fri Oct 24, 2008 12:02 am   Post subject: RE:word for word comparison

Alright the problem is the output will be "EOF" when one of the input files has an extra empty line and the other input file doesn't. Although Ive figured out why I'm getting an error ive yet to figure out how to correct my problem.



Here is a more readable version of the function
code:


int wordForWord(FILE *fp1, FILE *fp2)
{
        char firstWord[10];
        char secondWord[10];
        int numberOfFirst = 0;
        int numberOfSecond = 0;

        while(1)
        {
                if(!fscanf(fp1, "%s", firstWord))
                        return -1;
                if(!fscanf(fp2, "%s", secondWord))
                        return -2;
                numberOfFirst++;
                numberOfSecond++;

                if(feof(fp1) && !feof(fp2) && strcmp(firstWord, secondWord) < 0)
                {

                        printf("EOF on first file\n");
                        return CMP_EOF_FIRST;

                }

                if(feof(fp1) && !feof(fp2))
                {
                        printf("EOF on first file\n");
                        return CMP_EOF_FIRST;
                }

                if(feof(fp1) && !feof(fp2) && strcmp(firstWord, secondWord) > 0)
                {
                                printf("EOF on second file\n");
                                return CMP_EOF_SECOND;
                }

                if(feof(fp2) && !feof(fp1))
                {
                        printf("EOF on second file\n");
                        return CMP_EOF_SECOND;
                }

                if(feof(fp1) && feof(fp2) && strcmp(firstWord, secondWord) == 0)
                {
                        printf("files are equal\n");
                        return CMP_EQUAL;
                }

                if(strcmp(firstWord, secondWord) != 0)
                {
                        printf("These files differ at word %d\n", numberOfFirst);
                        return -3;
                }

        }
}
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: