Basically, I would like to know why this code doesn´t work. It appears that the value of strstr()
is always NULL
, as all this code ever does is "word not found"
.
I have already tried if (strstr(retezec,substring)!=NULL)
, but it doesn't work either.
int main()
{
FILE *files;
files = fopen("Knihovna.txt","rb+");
int i = 0;
while(fgetc(files)!=EOF){
i++;
}
//printf("%d",i);
rewind(files);
char *retezec;
retezec = (char *)malloc(i);
fread(retezec, i, 1, files);
puts("zadejte hledane slovo");
char *substring;
substring = (char *)malloc(50);
fflush(stdin);
fgets(substring,49, stdin);
char *found;
found = strstr(retezec,substring);
if(found){
printf("word found!");
}
else{
puts("word not found");
}
}