Comparing words in a file...
Hey guys. I'm in the stages of learning C. I'm making a program for practice. What I want the program to do is open a file, then compare all the words inside of it, then display what words that were used more than once. Well, I'm stumped. I've opened the file, and I read all the contents of it, and it can display each word. But, I'm not sure how to go about comparing each word. I think what I have to do is get them inside strings and use strcmp(), but I'm having some trouble. This is my code:
Code:
int main()
{
char ifilename[] = "c:/test.txt";
char test;
FILE *fp;
/* Opens the file, and displays an error if it can't */
if((fp = fopen(ifilename,"r")) == NULL) {
printf("Couldn't open for reading.\n");
exit(1);
}
/* Reads all the contents inside the file */
while((fscanf(fp,"%s", test)) != EOF) {
printf("%s\n", test);
}
fclose(fp);
return 0;
}
I tried to place each word on an individual line, and then compare each line, but I couldn't get that to work. How would I place each word inside a string?