Yet another question - Writing bytes to file
Posted on 2006-07-04
While I decide how to grade the other one - here is another question. It is based on other questions I have asked in the past but now that I have a better understanding of what I am trying to do, I can scale down the question a bit.
Okay, here goes.
Here is my implementation to generate a header to write to a file (Linux platform):
I am writing 4 bytes out to a file:
short field1 = 0;
int field2 = 1;
int field3 = 1;
int field4 = 1;
int field5 = 0;
unsigned int hdr = 0;
hdr=field1 << 16;
hdr += (field2 << 14) + (field3 << 11) + (field4 << 8) + field5;
fwrite( &hdr, sizeof(hdr), 1, fp);
I have another file where I am writing 6 bytes out -
The first byte -bits 5 and up 0
bit 4 is a flag - set to 1
bits 3, 2, 11
bits 1, 0 is 11
The next 5 bytes - setting values of 4 bits, then 20 bits, then 4. Then 12 bits. -
Totalling - 40 bits
My question is - do I have the flexibility to use unsigned char - 1 byte then unsigned int (4 bytes) and set each one? Does it make a difference to use 5 byte unsigned char rather than the above?
How does "byte-ordering" impact this?
Maybe I should post this in the C programming area?