Bruteforce approach.
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int findInFile(FILE *fp, const char *str)
{
void *data;
long flength;
int result = -1;
fseek(fp, 0L, SEEK_END);
flength = ftell(fp);
fseek(fp, 0L, SEEK_SET);
data = malloc(flength);
if(data && fread(data, flength, 1, fp) == flength)
{
result = 0;
char *pos = memmem(data, flength, str, strlen(str));
if(pos)
{
printf("Match at %zu
", pos - (char *)data);
}
else
{
printf("String not found
");
}
}
free(data);
fseek(fp, 0L, SEEK_SET);
return result;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…