Hi!I'm new to C programming and I wanted to ask where is the error in this code:
I have a file text ( frase.txt),placed in the same folder and I have to search for a string in this text and if I find that string I have to print it.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
const char searchingString[20];
FILE *fillle= fopen("frase","r");
char *s;
char *token;
printf("Introduce the string:");
scanf("%s",searchingString);
while(fgets(s,128,fillle)!=NULL){
token = strtok(s ," ");
if(strcmp(token,searchingString) == 0)
printf("%s\n",searchingString);
else printf("0\n");
while (strtok!=NULL) {
token= strtok(NULL," ");
if(strcmp(token,searchingString) == 0)
printf("%s\n",searchingString);
else printf("0\n");
}
}
}
When I compile the file it doesn't give me any problem and when I try the program it prints on the monitor Introduce the string,but when I give it it says segmentation fault!
What's the problem?thanks!
Sorry for my English!