I've this struct and function:
typedef struct{
char *maze_size;
char *trophy;
int time_max;
char *moves;
}gameHistory;
The function receives the pointer gameHistory *gameHistoryReaded
to reserve memory, but don't work.
void loadGameHistory(char fileName[], gameHistory *gameHistoryReaded, int *statusCode){
/* Here send error */
*gameHistoryReaded = (gameHistory*)malloc(LENGTH_GAME_HISTORY*sizeof(gameHistory));
(*gameHistoryReaded).maze_size = (char *)malloc(MAX_STRING*sizeof(char));
(*gameHistoryReaded).trophy = (char *)malloc(MAX_STRING*sizeof(char));
(*gameHistoryReaded).moves = (char *)malloc(MAX_STRING*sizeof(char));
FILE *file = fopen(fileName,"rb");
char line[200];
char *token1;
if (file == NULL){
printf("\nError de lectura: archivo no encontrado\n");
*statusCode = 0; //Si se presenta algun error en la lectura del archivsetea en 0 (error)
exit(1);
}
fgets(line,200,file);
token1 = strtok(line,":");
strcpy((*gameHistoryReaded).maze_size, token1);
token1 = strtok(NULL,":");
strcpy((*gameHistoryReaded).trophy, token1);
token1 = strtok(NULL,":");
strcpy((*gameHistoryReaded).moves, token1);
token1 = strtok(NULL,":");
(*gameHistoryReaded).time_max = atoi(token1);
fclose(file);
}
int main(){
char routegameHistory[] = "game_history.txt";
int statusCode = 1;
gameHistory gameHistory;
loadGameHistory(routegameHistory, &gameHistory, &statusCode);
}
Code::Blocks shown me this:
error: incompatible types when assigning to type 'gameHistory
' from type 'struct gameHistory *
'
But I think the struct
are well declared,
other point,
I allocate memory for the int time_max
?