word for word comparison
Author |
Message |
dchini
|
Posted: 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;
}
}
}
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
dchini
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
dchini
|
Posted: 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;
}
}
} |
|
|
|
|
|
![](images/spacer.gif) |
|
|