I wrote the function below to find a pattern in a text:
bool match(char* patt,char* text){
int textLoc=0, pattLoc=0, textStart=0;
while(textLoc < (int) strlen(text) && pattLoc < (int)strlen(patt)){
if( *(patt+pattLoc) == *(text+textLoc) ){
textLoc= textLoc+1;
pattLoc= pattLoc+1;
}
else{
textStart=textStart+1;
textLoc=textStart;
pattLoc=0;
}
}
if(pattLoc >= (int) strlen(patt))
return true;
else return false;
}
As it appears, the function takes two parameters of the type char*. I would like to use this function to find a pattern in a binary file, what you suggest to carry out this issue?
There is no right or wrong here. The only difference I would consider here is to use buffer/size approach instead of strings.
You should also consider how you would like to read the file. Are you going to read the entire file to memory or are you going to read it in sections ?
If you are going to read it in sections, always save the last part of each section ( the size of your search pattern ) and append it to the beginning of your next section. This way the cuts-off for each section will be evaluated as well.