When variables are in a struct and written to a file, they are not
It should be reasonably safe if the structs are read in by the same
program, compiled with the same compiler on the same system.
You may need the restriction "same VERSION of the same program".
....
I think what the OP is trying to do is what's called checkpointing.
It's a feature supported on some high-performance batch-processing
systems (supercomputers), where a computational job can easily run for
weeks, and the system is likely to go down before the job finishes.
There's no way to do this in portable C (so it's off-topic here), but
some research into whether your system supports checkpointing might be
fruitful.
I am reminded here of the save technique used in some old text and
curses-based games such as Zork and Rogue. The "save game" code
wrote a new executable (no way this is portable) using the original
code from the original executable, and it constructed a data segment
as the entire range of memory from the symbol _etext to sbrk(0)
(and this is a thousand times WORSE), encrypted. Needless to say,
this was EXTREMELY unportable (and, I suspect, actually worked on
0 machines, but sort of worked on at least Vaxes and PDP-11s). The
code made assumptions about the relative positions of the text
segment, data segment and the malloc() arena.
I recall trying to get this working on a machine where the end of
the code segment came *AFTER* the end of the malloc arena (Tandy
6000 / 68000-based). Also, it had a fixed-size stack, which came
between the data segment and the malloc arena. Nothing like trying
to encrypt the stack while using it. The result was not pretty.
But I eventually got it to work almost as well as the original did.
One of the larger problems with this code is that a save/restore
cycle saved and restored variables from library code that it knew
nothing about. Saving a game using one curses terminal type and
restarting it on another was problematical. A few library "initialize
once only" flags got restored as initialization having been done,
but the effect of the initialization (e.g. opening a file) was
reversed by the save/restore cycle, or, in the case of the curses
code, kept the old terminal type instead of using the new one.
Attempting to free() memory that was malloc()ed before a save/restore
cycle but which had been turned into static data also caused trouble
sometimes, but mostly, it just leaked memory.
Every save/restore cycle grew the process by including what was
formerly malloc()ed memory in the new data segment. After maybe
15 or 20 save/restore cycles, it got too big and tended to crash,
just when you got a good game going and were getting a high score.
Someone tried converting this to X. Something in the X libraries
objected violently to the brute-force save/restore method, and
a saved game aborted immediately when restarted.
I believe some of the newer (but still pretty old) games of similar
type, such as "nethack", actually make a point of saving only
variables that they know about. I don't think it can deal with
the situation of reloading a game into a new version where the
saved structures have changed size, or type, or values of some
of the "enum-like" variables got rearranged.
Gordon L. Burditt