Here's a thought: Why are you even reading entire words?
Code:
int skipword( FILE* fp )
{
char c;
int i=0;
while( !feof(fp) ) { c=fgetc(fp); if( isspace(c) ) break; i++; }
return i;
}
int getwordlength( FILE* fp1, FILE *fp2 )
{
char fc1, fc2;
maxLength=0,word2=0,word1=0;
do
{
fc1 = fgetc( fp1 );
fc2 = fgetc( fp2 );
if( !isspace( fc1 ) ) word1++;
if( !isspace( fc2 ) ) word2++;
if( isspace( fc1 ) && !isspace( fc2 ) )
{ //word1 has ended, word2 has not
word2+=skipword( fp2 );
if( word2 > maxLength ) maxLength = word2;
word1=word2=0;
}
else
if( !isspace( fc1 ) && isspace( fc2 ) )
{ //word2 has ended, word2 has not
word1+=skipword( fp1 );
if( word1 > maxLength ) maxLength = word1;
word1=word2=0;
}
// NOTE: Here, due to 'skipword', it is possible for
// potentially odd behavior. That is to say, you can
// no longer compare 'fc1' and 'fc2' at this point in
// the loop, since they are not updated. Thus:
if( isspace( fc1 ) && isspace( fc2 ) )
{ // here, we have not called skipword, if this case happens
// however, both words are the same length
if( word1 > maxLength ) maxLength = word1;
word1 = word 2 = 0;
}
}
while( !feof( fp1 ) && !feof( fp2 ) )
Ok. There are flaws with this code. However, I'll let you decide if this is something you want to continue with. The basis of this example is that you can compare both of these files without even allocating any memory (aside for loop counters and crap).
You can do it. Basicly, read both files and get the longest word length. You could do this even easier, if you just read one file at a time:
Code:
int wordlength( FILE*fp )
{
int length=0, longest=0;
do {
if( isspace( fgetc( fp ) ) {
if( length > longest ) { longest=length; length = 0; }
else
length++;
} while( !feof( fp ) );
return longest;
}
Run the first file through this, then the second:
l1 = wordlength( fp1 );
l2 = wordlength( fp2 );
longest = l1 > l2 ? l1 : l2;
Now if you need to, you can make your buffers:
char *buf1 = malloc( sizeof(char) *(longest+1) );
char *buf2 = malloc( sizeof(char) *(longest+1) );
However, you do not _need_ to allocate any buffer space if you play your cards right.
Quzah.