As already commented, time_t
can be virtually anything, the description even changed with versions of the standard, so the only thing you know is that it is a number. I'd say the only portable way to write it to a file is to read the bytes of its representation and store them (*), e.g. in hex format, or directly as binary using fwrite()
. For hex format, you could alias it with a char *
and read the bytes by indexing from 0
to sizeof(time_t) - 1
.
But then, you don't need the exact return value of time()
for what you want to achieve, you only need the seed that was actually used! If you look at the srand()
function, it takes an unsigned int
. So just convert whatever time(NULL)
returns to an unsigned int
first and store that.
Note that the inner workings of the PRNG aren't specified in the standard either, so if you obtain a logfile from a user, there's no guarantee you can reproduce the same sequence of random numbers from it if he linked against a different C library.
(*) This of course is not portable at runtime between different implementations, as they will have different representations. I guess the question isn't about that, but for completeness, if you really want to store a time value, the way to go would be to store e.g. the components of a struct tm
or use something like strftime()
.