Bit-by-bit reading fom Memory

Hi,

If I load a binary file into memory how do I read it bit-by-bit?

ifstream file ("my.bin", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();

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?
Hi,

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.

E.g.,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const int 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.
...............................
Thank you for your help. Could you please clarify a few points as this is advanced stuff for me?

The entire file is read into the cache.

When the search pattern is found it is written to the first byte of the cache and all previous data searched is abandoned.

Can I now access the next 80 data bits from cache.arr[1]?

I can now start a new search from for (int i = size*8 - pattern_length; i>=0; --i).

How is the size of the array reduced after each search?
Something I have found to be very useful when searching through files are the STL streambuf_iterators

http://www.cplusplus.com/reference/misc/iterator/istreambuf_iterator.html

Topic archived. No new replies allowed.