So, I now want to search for the pattern 10101 and when found extract the next 80 bits of data dividing it into various sizes then look for the next pattern. I can use file.get() but this returns the next character, but I want the next bit. Any ideas?
I think you can keep a local cache, read from file into that cache, and perform all the necessary actions locally, if you want to search for an n-bit long pattern , you can bring the last (n-1) bits of cache to the beginning and then the rest part fill with new data from stream. Thus you will be able to search for any pattern.
constint size = sizeof(long);
union my {
char arr[size];
long buff;
};
char pattern = 21; // binary pattern 10101
int pattern_length = 5;
int match_pattern = 31; // binary 11111
my cache;
ifstream file ("my.bin", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
file.seekg (0, ios::beg);
file.read (&cache.arr[0] + 1, size-1); // the first byte always will be filled with last part from the cache
file.close();
}
// ..... make necessary search here
for (int i = size*8 - pattern_length; i>=0; --i)
{
if ((cache.buff >> i) ^ pattern = 31)
// .... you have found the pattern in (8*size - (5 + i)) -th bit from left
}
// ... move the last bits to the beginning
cache.arr[0] = (cache.arr[size-1] | 0) << 3; // writing the last 5 bits in the beginning of first byte of cache
// .. now you can read from stream in same way as above.
...............................