r/C_Programming • u/HouseMFD • May 20 '26
Question How to read/write a struct into a binary file?
I've tried to follow this answer on SO: https://stackoverflow.com/a/16997175/1984657
However I'm not able to load my character successfully. I do get something back, but when I try to print the name I get a string of unrecognizable characters.
My struct is:
struct Character
{
char name[8];
enum CharClass charClass;
short level;
};My logic to load or create the character:
#include <stdio.h>
#include <string.h>
#include "character.h"
#include "save_file_manager.h"
struct Character *startup()
{
struct Character *loadedCharacter = loadPlayerCharacter();
if (loadedCharacter != NULL)
{
printf("Welcome back, %s the %s\n", loadedCharacter->name, getClassString(loadedCharacter->charClass));
return loadedCharacter;
}
struct Character createdCharacter;
struct Character *createdCharacterPtr = &createdCharacter;
char name[NAME_LENGTH];
int charClass;
// logic to get name and charClass from user input
strcpy(createdCharacterPtr->name, name);
createdCharacterPtr->charClass = charClass;
createdCharacterPtr->level = 1;
return createdCharacterPtr;
}And my save, load and prompt to save functions are:
#include "save_file_manager.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
const char *SAVE_FILE_NAME = "./textrpg.sav";
int savePlayerCharacter(struct Character *character)
{
FILE *fptr;
fptr = fopen(SAVE_FILE_NAME, "wb");
fwrite(character, sizeof(struct Character), 1, fptr);
fclose(fptr);
return 0;
}
struct Character *loadPlayerCharacter()
{
FILE *fptr;
fptr = fopen(SAVE_FILE_NAME, "rb");
if (fptr == NULL)
{
return NULL;
}
struct Character *character = malloc(sizeof(struct Character));
int amountRead = fread(character, sizeof(struct Character), 1, fptr);
if (amountRead != 1)
{
return NULL;
}
fclose(fptr);
return character;
}
int promptSave(struct Character *playerCharacter)
{
printf("Would you like to save your character? (y/n)\n");
char save;
scanf(" %c", &save);
if (save == 'y')
{
savePlayerCharacter(playerCharacter);
}
return 0;
}I probably missed something obvious, but I don't see it. What am I doing wrong?
Solution: I'm allocating heap memory to the created character in startup now:
struct Character *createdCharacterPtr = malloc(sizeof(struct Character));Thank you everyone for your help.
22
u/MyTinyHappyPlace May 20 '26 edited May 20 '26
In the startup function, you're returning a pointer to a local variable (struct Character createdCharacter). Once the function returns, that variable is gone and the pointer is dangling.
You did it right when loading a character from file by malloc()ing space for it.
15
u/[deleted] May 20 '26
Even if you fix other problems listed here this is not portable between platforms because of endianness and possible struct padding.
If you want to implement this correctly write/read each field manually.
1
u/This_Growth2898 May 20 '26 edited May 20 '26
allocate memory for createdCharacterPtr on a heap
1
u/Maleficent_Bee196 May 21 '26
Normally, I include a header with the size of the saved object at the beginning of the structure. Now, all you have to do is read the first header using the
freadfunction, go back to the beginning of the file, and read using the size specified in the header.3
u/un_virus_SDF May 20 '26
in
startupyou return a pointer to a value on the current stack frame, so its lifetime end at the return.You can either : returns the struct by value instead of returning it by reference, return a heap allocated struct, or give a pointer to the function, and the value pointed by the pointer will be modified.
For exemple instead of heap allocating for Reading the struct you can do something like
And read would be
This avoid useless heap allocations.
1
u/MagicWolfEye May 20 '26
Well, did you check that the contents of the file are correct; did you check if errors got set (errno)
1
u/Shtucer May 20 '26
name field in your structure from file contain pointer to first element of array. Isnt?
1
u/dvhh May 20 '26 edited May 20 '26
could you check the return value of the fwrite call, in doubt I would recommend creating a test file for the load function. As it is I can hardly see what is the issue, will test on my side.
I would also recommend checking the output file with xxd if available.
closer inspection of the code of "startup" if you create a new character you returna stack allocated struct, which means that the memory could be overwritten by any other stack usage before you call "savePlayerCharacter". Kindly malloc "createdCharacter" on the heap before writing and returning it
1
u/SmokeMuch7356 May 20 '26
This is a problem - the thing that
createdCharacterPtrpoints to - the variablecreatedCharacter- ceases to exist afterstartupreturns. This is exactly the same as writingwhich should trigger a diagnostic in almost any modern compiler since you're trying to return the address of a local variable.
You have three choices here:
Dynamically allocate your return object:
Return your created character by value, rather than using a pointer:
Pass a pointer to a
struct Characteras a parameter and write to it:1
u/Mundane_Prior_7596 May 20 '26
Apart from the memory lifetime you got solved, the other thing is that saving structs in files like that may work temporarily for yourself for a while but if you want portability and maintainability you need something like BSON because what you are doing will bite you sooner or later. Hint: padding pragmas and endianness will bite you. As well as the trick with variable length array as last member and other stuff. Or reading the file from Python. So do it your way but don’t show it to people :-)
-1
u/OddConsideration2210 May 20 '26
name[8] only keep a pointer.