reading binary files

Started by
1 comment, last by dense 22 years, 2 months ago
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?
Advertisement
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 filefwrite(&Frame1, sizeof(tFrame) - sizeof(int *), fOutput);fwrite(Frame1.Array, sizeof(int), Frame1.ArraySize, fOutput);// loading from filefread(&Frame1, sizeof(tFrame) - sizeof(int *), fInput);fread(Frame1.Array, sizeof(int), Frame1.ArraySize, fInput);  


Hope that helps.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Works perfect, thanks a lot.

This topic is closed to new replies.

Advertisement