Parsing Binary

I have a binary file a particular format.
It contains the Length Bytes and the Type bytes i.e the first four bytes if the file indicate the length of the Type which is to follow.

for eg, if the int value of the first four bytes is 80, then it means that the length of the following "Type" is 80. after i read the 80 bytes, the next four bytes again refer to the length of the next "Type"

I need to open the binary file, read the first four bytes, and then get the obtain the the bytes equaling the length obtained and keep doing this till the end of the file.

The problem i am having is that sometime the length of the file that it detects it wrong and that throws off the entire reading of the file

This is how i am reading the 4 bytes for length and then getting the long value

fread (Len, 1, 4, fp_bin);
long Length =(((((Len[0] << 8) + Len[1]) << 8) + Len[2]) << 8) + Len[3];
where Len is the char array containing the 4 bytes

this is how i am reading in the next 'Length' number of bytes
read(Data, 1, Length, fp_bin);

Are you trying to correct "Endianness" with this?

=(((((Len[0] << 8) + Len[1]) << 8) + Len[2]) << 8) + Len[3];

Otherwise, I do not see what you are trying to do.

length = *((long *)Len&0xffffffffffffffff) ,should give you correct length (on n/w endian machine)