I understand that this is an old post, but it occurs when people look for this information, so I will give my own solution.
This is basically what I used in one of my games. I actually have some additional special library code in my own dialog function, but that is essentially it. I write data individually. I donโt think I have a structure for this data, but there is no difference, just write your individual structure elements separately. As already mentioned, itโs better.
// returns 1 if successful, 0 if not int savemap(const char *map_name) { FILE *file = NULL; // open the file in write binary mode file = fopen(map_name, "wb"); // always check return values to see if it was opened okay if(file == NULL) { fprintf(stderr, "Error opening file for writing.\n"); return 0; } // write file ID, version and pills fwrite(MAP_ID, sizeof(char), strlen(MAP_ID)+1, file); fwrite(&MAP_VER, sizeof(unsigned char), 1, file); fwrite(&pills, sizeof(unsigned short), 1, file); // write the map data (unsigned int map[315]) fwrite(&map, sizeof(unsigned int), 315, file); // never forget to close the file fclose(file); return 1; }
Neil roy
source share