For a small todo program that I am writing, I have timestamps that are of this form
time_t t = time(NULL);
and are saved every time a task is entered to denote the time that it was entered.
I want to store the tasks to a plain text file, so that the state can be saved and restored. How should I store the timestamps to the text file and how should I get them back in my program after reading the text file?
Convert the
time_ttostruct tmusinggmtime(), then convert thestruct tmto plain text (preferably ISO 8601 format) usingstrftime(). The result will be portable, human readable, and machine readable.To get back to the
time_t, you just parse the string back into astruct tmand usemktime().For reference:
Code sample: