I want to write a program to calculate the no. of bytes between the first binary pattern (110) to the next 110 binary pattern. i know i probably have to make use of:


Do i need to use a pointer or something to tell the program where to start counting? Or is there a better way?


Unless you need something special, it would be easier to convert the bits to a string and do string searches. Then the problem is trivial.

Hi Narue,
I have already got the code of searching 001 byte pattern ready. So i was thinking how do i use this info to calculate the no. of bytes between two 001 byte pattern.


using namespace std;

bool isH264(ifstream& stream)
	int numZeros = 0;
		char c = stream.get();
		if(c == 0)
		else if(c == 1 && numZeros == 2)
			return true;
			numZeros = 0;
	return false;

int main(int argc, char *argv[])
	ifstream file("MOV_0003.mp4", ios::binary);
		cout << "This may be a H.264 file" << endl;
		cout << "This is not a H.264 file" << endl;
	return 0;

Nevermind, I was thinking bit patterns instead of bytes, but you asked about bytes. This is much easier, just begin incrementing a counter after you find the first matching byte and stop when you find the second. For example:

#include <iostream>
#include <cctype>

int main()
    bool found_start = false;
    int range = 0;
    char c;

    while (std::cin.get(c)) {
        if (std::isspace(c)) {
            if (!found_start)
                found_start = true;
            else {
                std::cout<< range <<'\n';
                found_start = false;
                range = 0;
        else if (found_start) {
This article has been dead for over six months. Start a new discussion instead.