Created
October 13, 2016 17:15
-
-
Save peat-psuwit/35122bea89667f287d44200df5ae8979 to your computer and use it in GitHub Desktop.
Wrong way to update binary file (don't do this at home)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stddef.h> | |
typedef struct { | |
char name[128]; | |
int max_hp; | |
int current_hp; | |
} Charactor; | |
#define SAVE_FILENAME "gamedata.bin" | |
int main() | |
{ | |
FILE *fp = fopen(SAVE_FILENAME, "rwb"); | |
Charactor c; | |
if (!fp) { | |
perror(NULL); | |
return 1; | |
} | |
while (fread(&c, sizeof(Charactor), 1, fp)) { | |
c.max_hp = 9999; | |
fseek(fp, -sizeof(Charactor), SEEK_CUR); | |
fwrite(&c, sizeof(Charactor), 1, fp); | |
fflush(fp); | |
} | |
fclose(fp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment