Welcome! Log In Create A New Profile

Advanced

(Solved!) Problem reading struct from file [fread, struct, fopen, rb]

Posted by Slappy 
(Solved!) Problem reading struct from file [fread, struct, fopen, rb]
August 07, 2009 04:24PM
Hi
I am porting game from PC to Wii
I have this struct:
typedef struct
{
	char				stav_pohybu;		
	char				stav_aktivace;		
	char				pozice40[2];		
	char				pozice41[2];		
	int				pozice800[2];	
	
	int				g_size[2];		
	int				g_ani_start[2];	
	int				g_ani_curr[2];		
	int				g_ani_orient;		
	char				g_ani_frame;		
	char				g_ani_speed;		
	char				g_ani_poc;			
	int				g_ani_framemax;		
	int				g_ani_posun[30][2];	
	char				poc;				
		
} Tactors;

Tactors					ac[2]; // << here I want to load data

And I wanna to load into ac from file
This is my code which works perfectly on Win-PC:
void load_room_object_data(int room)
{
	char* FileName = "data/rooms/object.dat"; // File exists, I am sure
	FILE* File = NULL;
		
	File = fopen(FileName, "rb");
	long size_ac = sizeof(ac);
	fseek(File, room*(size_ac),SEEK_SET);
	fread (&ac, size_ac, 1, File); // << here is reading from file
	fclose(File);
}

This function on PC [Windows] works fine and game run correctly, but on Wii I have random values in ac.
I had no exception, game is still running but values which I expected are not loaded.
What should I do?



Edited 1 time(s). Last edit at 08/07/2009 09:16PM by Slappy.
Re: Problem reading struct from file [fread, struct, fopen, rb]
August 07, 2009 06:07PM
The issue is endianess. After you load everything into the struct you'll have to convert the int values. Here's an example :-

int swap4 (int n)
{
   return (((n&0x000000FF)<<24)+((n&0x0000FF00)<<8)+((n&0x00FF0000)>>8)+((n&0xFF000000)>>24));
}

void load_room_object_data(int room)
{
	char* FileName = "data/rooms/object.dat"; // File exists, I am sure
	FILE* File = NULL;
		
	File = fopen(FileName, "rb");
	long size_ac = sizeof(ac);
	fseek(File, room*(size_ac),SEEK_SET);
	fread (&ac, size_ac, 1, File); // << here is reading from file
	fclose(File);

        // for all int values do the following ... e.g
        ac.g_ani_framemax = swap4(ac.g_ani_framemax);
    
}
Sorry, only registered users may post in this forum.

Click here to login