:
int search(const char *content, const char *search_term)
{
char *result = strstr(content, search_term);
if(result == NULL) return 0;
return (int)(result - content);
}
result content - , . , , ?
, . 0 , , 0. -1, :
if(result == NULL) return -1;
Further, however, you can see how this method is just a wrapper for functionality that you do not even use later. If all you care about mainis whether the string found, just completely remove this function and write mainas follows:
int main(int argc, char *argv[])
{
FILE *file;
char line[BUFSIZ];
int linenumber = 1;
char term[20] = "hello world";
file = fopen(argv[1], "r");
if(file != NULL) {
while(fgets(line, sizeof(line), file)){
if(strstr(line, term) != NULL) {
printf("Search Term Found at line %d!\n", linenumber);
}
++linenumber;
}
}
else {
perror(argv[1]);
}
fclose(file);
return 0;
}
source
share