I am completely amateur when it comes to C, and I am having trouble trying to write this piece of code. I want it to check a text file for any line that matches a given line.
For example, if "stackoverflow" was in a text file and the input string was "www.stackoverflow.com", it should return a positive match.
But he is currently looking for a string inside a text file, which is the opposite of what I want. I would appreciate any tips / advice!
int Check(char *fname, char *str) { FILE *file; int i = 1; int r = 0; char temp[1000]; if((file = fopen(fname, "r")) == NULL) { return(-1); } while(fgets(temp, 1000, file) != NULL) { if((strstr(temp, str)) != NULL) { printf("Host name found on line: %d\n", i); printf("\n%s\n", str); r++; } i++; } if(r == 0) { printf("\nHost name not blocked.\n"); } if(file) { fclose(file); } return(0); }
source share