Reputation: 21
I'm currently trying to set up my code so, upon exit, it will store the user's data. Upon using the program again, the data will be restored right where they left off. This is the only time data will need to be stored.
I'm rather new to data storage, when I practiced it I was able to store strings, ints, and bool types, but I had trouble when storing a tm value, getting the following error:
no operator found which takes a right-hand operand of type 'tm' (or there is no acceptable conversion)
Along with four int values, this is the data I need to store:
map<string, Date*> m_dates;
map<string, Tracker*> m_tasks;
map<string, vector<tm>> m_taskreports;
map<string, tm> m_datereports;
map<string, Goal*> m_goals;
Date, Tracker, and Goal are classes. Is there an easy way to store and retrieve this data?
Upvotes: 0
Views: 174
Reputation: 1978
You can probably save yourself a lot of trouble down the track by storing time stamps as numerical values (as returned by the time
function) instead of a tm
struct. This will also come in handy should you need to sort something in time order.
If you do need to access day, month, year and so forth then you can convert the numerical value back to a tm
struct when required (see, for example, http://www.cplusplus.com/reference/ctime/localtime/).
Before you use the time
function have a look at http://www.cplusplus.com/reference/ctime/time/.
As for general serialization, please see Rietty's answer.
Upvotes: 1
Reputation: 1156
You should take a look at serialization.
In computer science, in the context of data storage, serialization (or serialisation) is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer) or transmitted (for example, across a network connection link) and reconstructed later (possibly in a different computer environment).[1] When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward. Serialization of object-oriented objects does not include any of their associated methods with which they were previously linked.
This can be a tricky thing to get right, so I suggest using an existing solution. Many libraries can be found on Github.
Upvotes: 2