> Jeremy Coulter wrote:
> 
> Hi all.
> I have been doing some data conversion for work last few days, and one
> thing I to do is searching for some text in a file, and thenget the offset
> to the text. (the file is binary not text)
> 
> Woulsd I be best to read in say 1024 bytes into a buffer, then search the
> buffer for the text I want?

The only problem with that is if the text you're searching for spans two
reads.  For example, if the text string is 20 bytes long and starts at
offset 1020 in the file then you might not find it.

To work around this, create a buffer that's (block_size + length_of_string)
bytes long.  For each block copy the top (length_of_string) bytes into the
bottom of the buffer, and read the block from the file into the buffer at
offset(length_of_string).  This way you canbe sure that the data is always
going to be fully in the buffer.

Of course if you're searching for a large string then it might be better to
just look for the start of it, then do a full check at that position in the
file.  Or once you've found the first bit make sure the next part is there,
etc.


In C++ it'd look something like this:

int SearchFile(TFileStream* file, AnsiString text)
{
int len = text.Length();
char* buffer = new char[1024 + len];
  memset(buffer, 0, 1024 + len);
  file->Position = 0;
  while (file->Position < file->Size)
  {
    memcpy(buffer, buffer + 1024, len);
    file->Read(buffer + len, 1024);

    for (i = 0; i < 1024; i++)
      if (AnsiString(buffer + i, len) == text)
        return (file->Position - 1024 - len + i);
  }
  return -1;
}

Hopefully that gives you an idea of what I'm talking about.

-- 
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur!"
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"

Reply via email to