I too am having the same problem.
I addressed it the way Adak mentioned, using strstr. It worked and was easy to implement.
However, using strstr returns even partial matches, which I can't have. e.g. Searching for "the" returns matches on "there" and "therefore" for instance, which is not ideal.
I attempted to use sscanf to parse the string returned by fgets, then compare using strcmp, but it stops immediately upon reaching the first whitespace!
For instance, my code:
Code:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char line_storage[100], buffer[100];
int check, line_num = 1;
FILE *input = fopen(argv[1], "r");
while( fgets(line_storage, sizeof(line_storage), input) != NULL ) {
check = 0;
sscanf(line_storage,"%s",buffer);
if(strcmp(buffer,argv[2]) == 0) check = 1;
if(check == 1) printf("Word found on line %d ", line_num);
line_num++;
}
return 0;
}
correctly scans the input text file, but when it reaches sscanf it only parses the first word and then stops.
Is there any way to use sscanf to parse each word in the array line_storage, moving them to buffer and comparing against the test word?