Archived

This topic is now archived and is closed to further replies.

reading binary files

This topic is 5828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m having a little trouble here reading from a binary file (.md2 specifically). I''m using C style I/O here. I have a structure to hold the frame information, and inside the frame information I have an unknown sized array which will be dynamically created before reading from the file. The trouble I''m having though is that the structure is being filled correctly, except for the array I created. Here''s what I''m using now:
	fseek(data, header.offsetFrames, SEEK_SET);
	fread(frames, sizeof(frame), header.numFrames, data);
 
frames is the variable to store the info, and frame is the structure name. Now, I think the problem is that the since the structure holds a pointer and not an actual array, the sizeof doesn''t return the right size. I''ve tried a number of different sizeof() combinations, but nothing is working. Is there any way to do what I''m trying to do? Does this make sense?

Share this post


Link to post
Share on other sites
Since you''re array will be dynamic, then you''ll have to store the size of the array saved int the file somewhere in the file. Sorta like this:

  
typedef struct
{
int framex;
int framey;
... (whatever)
int ArraySize;
int *Array;
} tFrame;

tFrame Frame1;

// saving to file

fwrite(&Frame1, sizeof(tFrame) - sizeof(int *), fOutput);
fwrite(Frame1.Array, sizeof(int), Frame1.ArraySize, fOutput);

// loading from file

fread(&Frame1, sizeof(tFrame) - sizeof(int *), fInput);
fread(Frame1.Array, sizeof(int), Frame1.ArraySize, fInput);


Hope that helps.

Share this post


Link to post
Share on other sites