Re: FileStream Advice : Find a pattern of bytes starting at end of File

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Mar 22, 10:04 pm, "Russell Mangel" <russ...@xxxxxxxxx> wrote:
Hi,

I am using VS2008 SP1 and C#.

I would like to search/find for a pattern of bytes, starting from
the *end* of the file. The return value would be the Int64
position of where the pattern of bytes was found. The files I
am searching are large 450MB each. The reason I wish to
search backwards is that the target bytes are always at the end
of the file, I wanted to avoid searching through 445MB of data,
and then finding the information in the last 1MB. I suppose I
could seek forward to a position in the file which is close to
the end of file, and search forward from there.

Assuming that we search backwards, is there an easier way
to accomplish my goal without inheriting from Stream?

I don't see what you're hoping to gain at all by inheriting from
Stream, actually.

Maybe I don't understand how to use Filestream?
From what I can tell using FileStream, (Which seems to want
to go forward), we could Seek to position, read byte(s), Seek
back to previous position -1, read byte(s), (repeat).... Both
of the methods Read() and ReadByte() always advance the
position forward. This is pretty awkward. Or maybe I am
missing something simple?

No, that's just how things are. Files are optimized for reading from
beginning to end, not vice versa :) naturally, doing it backwards is
less convenient.

I don't know much about I/O intricacies here, but my gut feeling tells
me that doing all those Seek() calls isn't going to speed things up.
You _may_ get better performance if you instead read large chunks from
the end of file (one Seek/Read per chunk), and then do a normal
forward search within those. But take that with a grain of salt, and
do some profiling to verify this theory.
.



Relevant Pages