RTFM helped with this one, it wasn't clear in the notes that the last variable taken was for the size of array's.
If anyone get's stuck I have included the code I wrote to illustrate a simple saving algorithm in SDL2
struct STRUCT_Data
{
int INT_health;
float FLOAT_speed;
};
void FUNC_create_and_save_struct_array_to_binary_SDL()
{
///SET THE PATH
const char* CONST_CHAR_file_ref= "saves/SAVE_save_struct_array_to_binary_SDL2.bin";
///DECLARE THE VARS
size_t INT_array_maximum= 3;
size_t INT_array_maximum_loaded= 0; ///ONLY TO SHOW IT'S NOT USING THE PREVIOUS MAX
///FILL THE STRUCT WITH GENERIC CHUFF
STRUCT_Data STRUCT_current_save[INT_array_maximum];
STRUCT_current_save[0].INT_health= 10;
STRUCT_current_save[0].FLOAT_speed= 33.333;
STRUCT_current_save[1].INT_health= 255;
STRUCT_current_save[1].FLOAT_speed= 3.0;
STRUCT_current_save[2].INT_health= 6;
STRUCT_current_save[2].FLOAT_speed= 1.55;
///SAVE THE DATA TO A BINARY FILE
SDL_RWops *FILE_data_write = SDL_RWFromFile( CONST_CHAR_file_ref, "wb");
if( FILE_data_write ==NULL )
{
///ON FAIL RETURN NOTHING TO EXIT THE FUNCTION
return;
} else {
SDL_RWwrite( FILE_data_write, &INT_array_maximum, sizeof(size_t), 1 );
SDL_RWwrite( FILE_data_write, &STRUCT_current_save, sizeof(STRUCT_Data), INT_array_maximum );
SDL_RWclose( FILE_data_write );
}
///ZERO OUT THE VALUES SO YOU KNOW IT LOADS BACK IN OKAY
STRUCT_current_save[0].INT_health= 0;
STRUCT_current_save[0].FLOAT_speed= 0.0f;
STRUCT_current_save[1].INT_health= 0;
STRUCT_current_save[1].FLOAT_speed= 0.0f;
STRUCT_current_save[2].INT_health= 0;
STRUCT_current_save[2].FLOAT_speed= 0.0f;
///LOAD THE DATA BACK IN TO THE PROGRAM
SDL_RWops *FILE_data_read = SDL_RWFromFile( CONST_CHAR_file_ref, "rb");
if( FILE_data_read ==NULL )
{
///ON FAIL RETURN NOTHING TO EXIT THE FUNCTION
return;
} else {
SDL_RWread( FILE_data_read, &INT_array_maximum_loaded, sizeof(size_t), 1 );
SDL_RWread( FILE_data_read, &STRUCT_current_save, sizeof(STRUCT_Data), INT_array_maximum_loaded );
SDL_RWclose( FILE_data_read );
}
///CHECK IT DISPLAYS OKAY
std::cout << "STRUCT_current_save[0].INT_health: " << STRUCT_current_save[0].INT_health << std::endl;
std::cout << "STRUCT_current_save[0].FLOAT_speed: " << STRUCT_current_save[0].FLOAT_speed << std::endl;
std::cout << "STRUCT_current_save[1].INT_health: " << STRUCT_current_save[1].INT_health << std::endl;
std::cout << "STRUCT_current_save[1].FLOAT_speed: " << STRUCT_current_save[1].FLOAT_speed << std::endl;
std::cout << "STRUCT_current_save[2].INT_health: " << STRUCT_current_save[2].INT_health << std::endl;
std::cout << "STRUCT_current_save[2].FLOAT_speed: " << STRUCT_current_save[2].FLOAT_speed << std::endl;
}