I've been trying my hand at coding in C to remove myself from the syntatic sugar of higher level languages. Inevitably I have encountered a brick wall on what would seem like a trivial task in a high level language, by trying to confirm if a sequence of characters is within an array of characters.
# include <stdio.h>
# include <string.h>
int main(int argc, char* argv[])
{
FILE* file = fopen("test.txt", "r");
char line[256];
char *characters = "learning";
while (fgets(line, sizeof(line), file))
{
if (strchr(characters, *line));
{
printf("found\n");
}
printf("%s", line);
}
fclose(file);
return 0;
}
Output
found Hi my name is Chris found I am currently learning the C programming language found I am currently in a pickle
The program is supposed to get one line from the text file at a time and check whether the sought after string is within it. And the current condition evaluates true for every line. And in the case of the sample text, the string "learning" can only be found on one of the lines. For the sake of testing: I tried to use a completely different string to search for, one that doesn't appear on any of the lines. And I get the same output as above.
I'd appreciate it if someone could provide me with insight that could lead me to a solution.
Thanks!

New Topic/Question
Reply



MultiQuote



|