Why I can not get right number?

Hi,

I have raw data in a binary file. I try to count how many Byte meets this condition: its value equals 128, and the Byte after it 255 bytes equals 255. The final count should be 100 00. But the C++ program always give a less value, seems missing some byte that satisfies the condition. Anyone can help me with this problem. Thanks a lot!
the raw data can be downloaded here:https://notendur.hi.is/~jil3/Data/RawData.raw

The C++ codes are here:


#include "stdafx.h"
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
string filename;

cout<<"please give a file name: ";
cin>>filename;
cout<<filename<<endl;
const char *addr= filename.c_str();
unsigned int Size=0, framenumber=0;


FILE *fp;
int fSize=0,fSize0;
unsigned char* header;
unsigned int i=0;
long int k=0;


fp=fopen(addr, "r");
if(fp==NULL){ fputs("Read file error",stderr); exit(1);
}

fseek(fp,0,SEEK_END);
Size=ftell(fp);
rewind(fp);

header=(unsigned char*) malloc(Size);


fread(header,1,Size,fp);

while (i<(Size-256)){
if ((header[i]==128)&&(header[i+255]==255)){
framenumber=framenumber+1;
}
i=i+1;
}



cout<<"framenumber:"<<framenumber<<", Size: "<<Size<<endl;


return 0;

}

Thanks again!

with best regards
JL
Last edited on
Open file in binary mode, not text mode.
Hi MiiNiPaa,

Thanks for help. I add "b" in "fp=fopen(addr, "rb");". It shows error when read ing the data file.
I use VC++ 2010 and XP operation system.

regards
JL
Last edited on
What is the error message?
Thanks. I check other data file. It works. The previous one may be broken.
Thanks a lot!
Topic archived. No new replies allowed.