Hello.
I have a routine to search a drive for files of certain type to see if they contain a string of text. Just like Windows Explorer has.
Except min is extremely slow.
What mine does is goes char by char through a file and loads a sizeable buffer reading from the file to a buffer. Then that buffer is examined by CStrings Find member for the search string.
The reason I have to make it increment one char at a time is because sometimes the sought after string may be only partialy picked up by the buffer being read into.
Example:
Say I was looking for string "abc";
My text file has string 12abc123
Now if I search with a buffer sized 4 chars (Buff) and start at beginning to end of file in a while read loop the first loop over the file data will miss the string
while(fread(Buff..))
1=Buff==12ab
2=Buff==c123
and the results is not found.
So instead I use fseek and do the loop over and over
while(1)
{
fseek(fptr,x,0);
while(fread(Buff...))
{
if(Same)....break;return;
}
x++;
}
This works but is slow. Because not only am I going through each file char by char but also I am repeating reading larger chunks. Buff is actually determined by a percentage of the current file size. So at least the while fread loop is sped up as much as possible.
I also considered just reading the whole file into 1 buffer which would speed things up drastically. BUT the user may have extremley large files. I do not know what CString can handle as I will have to load the Buffer into a CString.
Also that does not seem like good form. Because who knows about future file sizes.
I am considering some sort of value system where I can assign values to the first loop of large buffer size accourding to how "CLOSE" each buffer cam to matching the search string.
Say if one buffer had 1 char another buffer had 2 chars (In same order) then it would get a 2 and so on...
Then the buffer with the largest similar count would be re-examined by extending it's length in order to read the whole possible text from the file. If it does exist then.
Any ideals would be apreciated. Sorry about the lack of points.
RJ
Here is the code.
int XXXXXX::StringFound(char *SearchSt, char *FileName)
{
CString CBuff;
CString Search=SearchSt;
int found =0;
::GetShortPathName(FileNam
e,FileName
,MYMAXPATH
-1);
//
//
////////////////Get the file size
long FileSize=0;
long x=0;
struct _finddata_t ptffblk;
long fhandle = _findfirst(FileName,&ptffb
lk);
if(fhandle==-1)
{
MessageBox("Failed to _findfirst",FileName);
return 0;
}
else _findclose(fhandle);
FileSize=ptffblk.size;
//////////////////////////
///////
//
long Size=0;
long Ms=MYMAXPATH;//arround 1,000
if(FileSize<Ms)Size=FileSi
ze;
else Size=Ms;
//
char *Buff= new char[Size];
//
FILE *fptr=NULL;
fptr=fopen(FileName,"rb");
if(fptr!=NULL)
{
while(1)
{
fseek(fptr,x,0);// start from beginning and offset x amount
while(fread(Buff,Size,1,fp
tr))
{
//MessageBox(Buff);
CBuff=Buff;
CBuff.MakeLower();
Search.MakeLower();
if(CBuff.Find(Search)!=-1)
{found=1;b
reak;}
}//endwhileread
x++;
if(Size==FileSize)break;
if(x>=FileSize || found)break;
}//endwhile
fclose(fptr);
delete[] Buff;
}//endifnotnull
return found;
}//endfunc