0

How do you store data to be used for a variable?

Is there a way to store the value of a certain variable in a file and then when the program is run again, it is read and that value is assigned back to that variable? If that is too complicated can someone tell me how programs "remember stuff"?

18th Dec 2019, 6:34 PM
ManWithACap
ManWithACap - avatar
4 Answers
+ 1
You can either write your own functions that read and write to files or you use libraries that automatically help you with certain formats such as XML or JSON. I once wrote a very simple config file handler that simply reads a file for properties and stores the values in a object. The file should look like this: property_name1=value1 property_name2=value2 ... For this I made class called conf that automatically loads this config file into memory when created and writes changes back when the destructor is called. You can check out the code here: https://gitlab.com/AaronErhardt/Vino/blob/master/src/file.cpp https://gitlab.com/AaronErhardt/Vino/blob/master/src/file.hpp Of course there are other solutions for this problem (e.g. you could write and load large data from binary files). Yet for configs text files are usually better because a human can read and modify them.
18th Dec 2019, 7:11 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
Yep that's how most programs work. They store their data in binary or text files (usually as JSON or XML) to load or modify it when needed.
18th Dec 2019, 6:50 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
Aaron Eberhardt I appreciate the help!👍
18th Dec 2019, 7:15 PM
ManWithACap
ManWithACap - avatar
0
Good to know. How would you do that? Aaron Eberhardt
18th Dec 2019, 6:56 PM
ManWithACap
ManWithACap - avatar