<分区>
基本上,我想知道为什么这段代码不起作用。 strstr()
的值似乎始终为 NULL
,因为这段代码所做的只是“word not found”
。
我已经尝试过 if (strstr(retezec,substring)!=NULL)
,但它也不起作用。
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");
}
}