You could try std::search() with istreambuf_iterator< unsigned char >.
That's very problematic. istreambuf_iterator< unsigned char >
will expect a basic_streambuf< unsigned char >, which isn't
defined by the standard (and you're not allowed to define it).
A number of implementations do provide a generic version of
basic_streambuf, but since the standard doesn't say what the
generic version should do, they tend to differ. (I remember
sometime back someone posting in fr.comp.lang.c++ that he had
problems because g++ and VC++ provide incompatible generic
versions.)
It would, I suppose, be possible to use istream_iterator<
unsigned char >, provided the file was opened in binary mode,
and you reset skipws. I have my doubts about the performance of
this solution, but it's probably worth a try---if the
performance turns out to be acceptable, you won't get much
simpler.
Except, of course, that search requires forward iterators, and
won't (necessarily) work with input iterators.
[...]
Maybe, rolling your own is not all that bad. You could read
the file in chunks (keeping the last three characters from the
previous block) and use std::search() on the blocks. With the
right blocksize, this could be really fast.
A lot depends on other possible constraints. He didn't say, but
his example was to look for 0x650A1010, not the sequence 0x65,
0x0A, 0x10, 0x10. If what he is really looking for is a four
byte word, correctly aligned, then as long as the block size is
a multiple of 4, he could use search() with an
iterator::value_type of uint32_t. For arbitrary positions and
sequences, on the other hand, some special handling might be
necessary for cases where the sequence spans a block boundary.
When I had to do something similar, I reserved a guard zone in
front of my buffer, and used a BM search in the buffer. When
the BM search would have taken me beyond the end of the buffer,
I copied the last N bytes of the buffer into the end of the
guard zone before reading the next block, and started my next
search from them. This would probably make keeping track of the
offset a bit tricky (I didn't need the offset), and for the best
performance on the system I was using then, I had to respect
alignment of the buffer as well, which also added some extra
complexity. (But I got the speed we needed
.)
If your OS allows memory mapping of the file, you could do
that and use std::search() with unsigned char * on the whole
thing. That could be the fasted way, but will leave the realm
of standard C++.
If the entire file will fit into memory, perhaps just reading it
all into memory, and then using std::search, would be an
appropriate solution. Or perhaps not: it's often faster to use
a somewhat smaller buffer, and manage the "paging" yourself.