Need Dev Help With Fread()


StrikerBlueI

Still Fresh
Joined
Jun 28, 2006
Messages
3
I have written a 3D Engine (not complete yet). It's called GMath3D. I am using the fread() function to load data from a polygon data file. This function seems to work flawlessly when used in my 3D editor in Windows, but when I try and load the polygon data into my gp2x program, my system freezes. The code used to load the data is shown below.

I have read elsewhere that fread can load arrays of struct (data types). When this code is used in my gp2x app, it seems to load data as all 0's (zeros or NULL); What might be the problem here.

void G3D_LoadModel(char* file, Model3D model, PolyPoint3D *tri, DeltaList3D *delta)
{
FILE *fp;
fp = fopen(file, "r");
fread(&model, sizeof(Model3D), 1, fp);
fread(tri, sizeof(PolyPoint3D), model.mPoints, fp);
fread(delta, sizeof(DeltaList3D), model.mPanels, fp);
fclose(fp);
}
 
Your not trying to load data from a file directly into a structure when it was written to the file under a different OS (from that structure) are you?

If you are, you may have fell foul of the invisible structure padding.

Check sizeof(Model3D), PolyPoint3D, DeltaList3D etc on both PC and Gp2X, and you may find the values differ, so you load corrupted info on the GP2X.
 
I made a mistake. Sorry.
I was using fopen(file, "r");
I should have been using fopen(file, "rb"); // Read binary data.

My program works correctly now. Thanks for the input.
 
StrikerBlueI posted on Mar 20 2007 at 11:49 AM said:
I made a mistake. Sorry.
I was using fopen(file, "r");
I should have been using fopen(file, "rb"); // Read binary data.

There is no such thing as a "binary" mode in Unix, so the b flag is ignored in the loader.

It might be useful in the program that saves the data, though.
 
Last edited by a moderator:
Yup, Windows definitely cares about the 'b' flag, and if not used, will change all 0x0A's in your output to 0x0D 0x0A.

The other suggestions are still valid though, so make sure your structure padding is the same on both PC and Gp2X.
 
Back
Top