I am trying to write a function that compares two text files (test1.txt and test2.txt) word by word, the function is supposed to print "files are equal", "files differ: word %d" (if word 13 then past 12 words were equal), and "EOF on [filename]" (the files were equal word by word but one of the files ended before the other).

The problems I am having are:

  • the EOF statement only prints if one of the files are completely empty
  • the files differ statement only occurs if the first word is different, otherwise it won't print the statement
  • the files are equal statment occurs only if the first word for each file are the same
int compare_word(FILE *fp1, FILE *fp2)
{
	size_t wordCount = 1;

	int isEqual = 1;

	unsigned int w1 = 0;
	unsigned int w2 = 0;	
	
	char word1[LINESIZE];
	char word2[LINESIZE];

		if(fscanf(fp1, "%s", word1) != EOF)
		{
			if(fscanf(fp2, "%s", word2) != EOF)
			{
			
			}
			else
			{
				isEqual = 0;
				printf("EOF on test2.txt");
				return 0;
			}
		}
		else
		{
			isEqual = 0;
			printf("EOF on test1.txt");
			return 0;
		}
		for(; w1 < strlen(word1) && w2 < strlen(word2); w1++, w2++)
		{
			if(word1[w1] != word2[w2])
			{
				isEqual = 0;
				printf("files differ: word %d", wordCount);
				return 0;
			}
		}	
		wordCount++;
		if(isEqual == 1)
		{
			printf("files are equal");
		}
	return 0;
}

the files differ statement only occurs if the first word is different, otherwise it won't print the statement
the files are equal statment occurs only if the first word for each file are the same

printf("files differ: word %d", wordCount);
return 0;

when you used the return statement, you ended the loop and function

printf("files differ: word %d", wordCount);
return 0;

when you used the return statement, you ended the loop and function

I removed that return statement, but as long as the first word for each file are equal, the "files differ" still won't print if the other words are not equal.

your not reading the file word by word... judging from the current code, it will only check the first word of the files

use a loop that will get the words from the file until it reaches the EOF

commented: Was very helpful and specific with the question at hand +0

I got my function to loop so I'll just post my code and mark it as solved. Thanks for your help zeroliken.

int compare_word(FILE *fp1, FILE *fp2, int i)
{
/*if i=0, then comparison is case sensitive. if i=1, then comparison is case insensitive */
	int wordCount = 1;
	int c = 0;	
	
	char word1[LINESIZE] = {0};
	char word2[LINESIZE] = {0};
	

	while(1)
	{
		if(fscanf(fp1, "%s", word1) != EOF)
		{
			if(fscanf(fp2, "%s", word2) != EOF)
			{                        
			}
			else 
			{
				printf("EOF on file2.txt");
                                return 0;
                        }
                } else if(fscanf(fp2, "%s", word2) != EOF)
			{
                                printf("EOF on file1.txt");
                                return 0;
                       
                	} 
		else 
		{
                	printf("files are equal\n");
                        return 1;
                }
		for(c = 0; c < (int)strlen(word1) || c < (int)strlen(word2); c++)
		{
			if(i == 0)
			{
				if(word1[c] != word2[c])
				{
					printf("files differ: word %d\n", wordCount);
					return 0;
				}
			}
			else if(i == 1)
			{
				if(tolower(word1[c]) != tolower(word2[c]))
				{
					printf("files differ: word %d\n", wordCount);
					return 0;
				}
			}
		}
	wordCount++;
	}
	printf("files are equal");
	return 1;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.