Search a binary file for a string... again! (its to slow)

From: spike (joan_at_ljungh.se)
Date: 02/28/04


Date: 28 Feb 2004 12:22:33 -0800

Im writing a program to search for a string in a binary file.
And it works. The problem is: It is sooo slow! how can i make it faster?
It takes 27 seconds just to search a 5 meg file.
I guess it has something to do with the strequal() function...

Btw, thanks to all of you who answered last time!

code:
-------------------------------------------------------------------------
#include <stdio.h>
#define MAXLEN 50
#define FALSE 0
#define TRUE !FALSE
#define STRSIZE 4

int strequal(char sText[],char sBuff[])
{
 int iRetval = TRUE;
 int i;
 for(i=0;i<STRSIZE;i++)
 {
         if(sText[i] == sBuff[i])
         {
            iRetval = iRetval && TRUE;
         }
         else
         {
            iRetval = iRetval && FALSE;
         }
 }
 return iRetval;
}

int main()
{
  FILE *fp;
  char sText[STRSIZE] = "name";
  char sBuff[STRSIZE];
  unsigned long pos;

  if((fp = fopen("demo.dem","rb")) != NULL)
  {
     while ((fread(&sBuff, sizeof(char), sizeof(sText), fp)) >= (sizeof(sText)))
     {
        if(strequal(sText,sBuff))
             {
                printf("a match!\n");
             }
             fgetpos(fp, &pos);
             pos = pos-(STRSIZE-1);
             fsetpos(fp, &pos);
     }
     fclose(fp);
  }
  else
  {
      printf("Could not open file");
  }
  return 0;
}
-------------------------------------------------------------------------



Relevant Pages