Check for a word in a text file line by line and when you found it,print the whole line,the program receives as command line argumets the text file and the word you are searching for. Here is what I've tried to do so far,but I have segmentation fault and I don't really know what I'm supposed to do.
int main(int argc, char const *argv[])
{
FILE *file;
if(argc!=3)
{
printf("error 1");
exit(1);
}
if((file=fopen(argv[1],"r"))==NULL)
{
printf("Error");
exit(1);
}
char line[100];
char *p;
while(!feof(file))
{
fgets(line,sizeof(line),file);
p=strtok(line," ");
while(strcmp(p,argv[2])!=0)
p=strtok(NULL," ");
if(strcmp(p,argv[2])==0)
{
printf("%s",line);
}
}
fclose(file);
return 0;
}