I have loaded a binary file into memory (using ios::binary). I need to search for 10101 (or 0x15), which I did by searching each value returned by file.get(). What I have just realised is that 10101 may be part in the first 8 bits of file.get and part in the next 8 bits of file.get, ofcourse I will miss this by my search method. Does anyone know of a better search method?
Any help is appreciated.
PS. I posted this in the Beginners Forum then I thought it may be better here. Please don't be annoyed that I have posted it in two forums.
I don't know of any method readily provided for this kind of thing. However, if you read two 8-bit bytes, there are only 6 possibilities of bit sequences containig the pattern. So it should not be too hard.
Not really important for real life (TM), remember that the size of a byte is implementation defined: (ISO 14882:1998)
The fundamental storage unit in the C++ memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set and is composed of a contiguous sequence of bits, the number of which is implementation-defined.
Therefore, the really platform-independent solution allowing to search for other patterns as well (which might be of arbitraty length) is to read the file bit-by-bit (for which you might need a wrapper function) and performing the matching then. For the storage of the bits (reading longer sequences of bytes increases the performance quite a bit) you can use a std::vector<bool>, which is optimized to use one bit per value.