Skip to content

Instantly share code, notes, and snippets.

@peat-psuwit
Created October 13, 2016 17:15
Show Gist options
  • Save peat-psuwit/35122bea89667f287d44200df5ae8979 to your computer and use it in GitHub Desktop.
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)
#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