I'm trying to parse a binary file.
The file consists of multiple header->record pairs.
The record length and type are stored in the header.
I want to be able to read the entire header into a buffer and then split it into the variables.
Unfortunately I can't figure out how to do it.
Code:
#include <stdio.h>
#include <stdlib.h>
#define HEADER_SIZE 4
typedef struct rHead
{
unsigned short int REC_LEN;
unsigned char REC_TYP;
unsigned char REC_SUB;
} recHead;
recHead readHeader(char *buffer)
{
recHead hdr;
/* no idea how to parse it */
return hdr;
}
int main()
{
FILE *fp;
fp=fopen("1.std","rb");
char *buffer;
buffer = (char *)malloc(HEADER_SIZE+1);
fread(buffer,HEADER_SIZE,1,fp);
recHead header = readHeader(buffer);
free(buffer);
return 0;
}