Tuesday, December 28, 2010

sweet

Ok, trusting fread to do the right thing with my FILE pointer was the right thing to do. The next fread call did indeed start from where the last one left off. I was able to move past the first overall file header and read in the channel headers. Not only that, but I was able to use information in the file header (the channel count) to tell fread specifically how far into the file to go.

Another important lesson (or rather, putting into practice what I have already read about).

stdio.h speaks of fread as this...

extern size_t fread (void *__restrict __ptr, size_t __size,
size_t __n, FILE *__restrict __stream) __wur;

Now, I don't know what 100% means, but I know that the first thing fread wants is a memory address. It LOOKS LIKE it wants a pointer, but when your PROTOTYPE has the pointer to some type you need to be passing the function a memory address to that type.

So, I used fread for two things - getting the overall file header (where there is one of) and getting the channel headers (which there are several of).

The file header and channel headers are declared as follows:

struct PL_FileHeader fileheader;

struct PL_ChanHeader chanheader[128];

and when I used fread for each, I used it as follows:

fread(&fileheader, sizeof(struct PL_FileHeader), 1, plxfilepointer);

fread(chanheader, channelcount * sizeof(struct PL_ChanHeader), 1, plxfilepointer);

You'll see in the second fread things are a bit different. The first argument fread wants is the location of a variable (in my case a structure) for the data to go to. fileheader is a structure, so I passed fread the address with &fileheader. chanheader is actually an ARRAY of structures, so all I had to do is pass it the name of the array - a shortcut of sorts. I say a shortcut because I can also say &chanheader and it means the same thing. The second argument is how much to dig into the file. For the fileheader I just want to dig in the amount of size that the fileheader is. For the chanheader I needed to dig in, but only for as many channels as the fileheader says I have. That was an easy number to extract (channelcount) and multiplication took care of the rest. Yes, this means I have 124 elements of that array unused, but I'm not ready to dynamically allocate that just yet.

No comments:

Post a Comment