I need to read a binary file byte for byte in blocks of 8 bytes. Then I need to check bytes 5 and byte 7 whether their value is zero. If it found a block with that criteria, printf should show me the entire 8 byte block.
Sounds pretty easy, but I didn’t get it to work as I expected.
I tried something like that, but without success:
unsigned char buffer[8];
FILE *file;
unsigned long fileLen;
//Open file
file = fopen("tcpstream-noframe.raw", "rb");
if (!file)
{
fprintf(stderr, "Unable to open file %s", "tcpstream-noframe.raw");
return 1;
}
for(int i=0; i++ ; i<9999) {
fread(buffer, 8, 1, file);
if(buffer[5] == 0 && buffer[7] == 0)
printf("%X %X %X %X %X %X %X %X\n",buffer[0], buffer[1], buffer[2], buffer[3],buffer[4], buffer[5], buffer[6], buffer[7]);
}
Any ideas? Thanks.
Your for cycle has no effect. The correct syntax for for cycle is
,which means that you probably switched loop_condition (i<9999) and increment_expression (i++).
Also, if you want to check bytes 5 and 7, in C zero-indexed array you should
check buffer[4] and buffer[6].