C's stdio routines have no idea of what a 'word' is. The closest you can have is to use fscanf()
to read sequences of characters separated by spaces:
int searchMatch(char *string, FILE *file) {
char buff[1024];
while (fscanf(file, "%1023s", buff) == 1) {
printf("BUFF=%s\n",buff);
if (strcmp(buff, string) == 0) return 1;
}
return 0;
}
This may or may not fulfill your definition of a word. Note that things like "Example test123" are interpreted as two words: "Example" and "test123".
Also, your original code would never work, because you didn't allocate space for buff
. fgets()
does not allocate memory for you, buff
must be a pointer to a valid allocated memory block.
Note that the loop condition was changed so that it implicitly stops when no more input is available - it is generally a good practice to let loops stop when the condition is false, rather than scattering a bunch of break
instructions in its body.