I am testing a custom structure where there is bitfield and a unsigned char * (that will be allocated later).
Here is the structure :
struct test {
unsigned int field1 : 1;
unsigned int field2 : 15;
unsigned int field3 : 32;
unsigned int dataLength : 16;
unsigned char * data;
}
The problem is when I tried to save this struct inside a file in hex.
For example :
int writeStruct(struct test *ptr, FILE *f) {
// for data, suppose I know the length by dataLength :
// this throw me : cannot take adress of bit field
int count;
count = fwrite( &(ptr->field2), sizeof(unsigned int), 1, f);
// this throw me : makes pointer to integer without a cast
count = fwrite( ptr->field2, sizeof(unsigned int), 1, f);
// same for length
count = fwrite( htons(ptr->data) , ptr->dataLength, 1,f);
// so , how to do that ?
}
The same problem goes to fread :
int readAnStructFromFile(struct test *ptr, FILE *f) {
// probably wrong
fread(ptr->field1, sizeof(unsigned int), 1, f);
}
So , how can I write/read struct like this ?
Thanks for your help
PS for fread, this could work if there wasn't these bitfields: How to fread() structs?