C question - headers in mmaped files
Michael Still
mikal at stillhq.com
Wed Apr 2 07:21:01 EST 2003
On Tue, 1 Apr 2003, Jepri wrote:
> map = (char *) mmap(etc etc)
> head = (header *) map;
> dat = (data *) map+64;
In addition to what everyone else said, lots of people solve this sort of
problem with a struct and a union. Checkout this code which probably wont
compile (pine needs a "compile my example" command):
typedef struct{
char magic[4];
int version;
float gronk;
} dattype;
typedef union{
dattype data;
char * raw;
} jpfile;
jpfile foo;
foo->raw = mmap(...);
foo.data.magic[0] = 'J';
foo.data.magic[1] = 'P';
foo.data.magic[2] = 'F';
foo.data.magic[3] = 'L';
...etc...
This lets you jump backwards and forwards between chars and real data. If
you only wanted the structure, ignore the union, cast into the struct at
mmap time, and then cast back to chars when you want to write it.
Cheers,
Mikal
--
Michael Still (mikal at stillhq.com) | Stage 1: Steal underpants
http://www.stillhq.com | Stage 2: ????
UTC + 10 | Stage 3: Profit
More information about the linux
mailing list