This code saves an actor to a file. After modifying the age, I load the actor back from file. What's strange is it still has the modified age. I would expect it to be 28 again? Edit: The loading function returns 1.
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct _skills { int short_sword; int axe; } SKILLS; typedef struct _actor { char name[20]; int age; SKILLS skills; } ACTOR; void Actor_Init(ACTOR *p_Actor, const char *name, int age) { strcpy(p_Actor->name, name); p_Actor->age = age; p_Actor->skills.short_sword = 50; p_Actor->skills.axe = 5; } int Actor_Save(ACTOR *p_Actor, const char *filename) { FILE *fp; fp = fopen(filename, "wb"); if (fp == NULL) return 0; fwrite(&p_Actor, sizeof(ACTOR), 1, fp); fclose(fp); return 1; } int Actor_Load(ACTOR *p_Actor) { FILE *fp; fp = fopen("actors.dat", "rb"); if (fp == NULL) return 0; fread(&p_Actor, sizeof(ACTOR), 1, fp); fclose(fp); return 1; } void Actor_Show(ACTOR *p_Actor) { printf("Name: %s\n", p_Actor->name); printf("Age: %d\n", p_Actor->age); printf("Sword: %d\n", p_Actor->skills.short_sword); printf("Axe: %d\n", p_Actor->skills.axe); } int main(void) { ACTOR PC; Actor_Init(&PC, "Test", 28); Actor_Save(&PC, "actors.dat"); PC.age = 29; Actor_Load(&PC); Actor_Show(&PC); return 0; }



LinkBack URL
About LinkBacks


