what is the simplest way to store variables persistently? In my case i just want to store some strings for a config which can be accessed when my program gets restarted. I have read about writing and reading txt files, but I was wondering if there is not an even more convienient way to do it?
Asked
Active
Viewed 77 times
0
-
4Saving and loading the data from a text file is about as simple as it gets. – Retired Ninja Oct 03 '22 at 12:47
-
There are many available libraries you can integrate into your code to use (create, maintain, read from and write to) initialization files., which IMO is the simplest and most portable way to simply store variable states. [Here is an example library](https://github.com/ndevilla/iniparser) – ryyker Oct 03 '22 at 13:18
-
@RetiredNinja okay then I will stick to this. What is the best path to store theese files in? – CodeEnjoyer Oct 03 '22 at 15:46
-
1That's a more complicated question. :) On Windows you'd probably want to create a directory under the AppData directory. https://stackoverflow.com/questions/3964124/how-to-get-the-appdata-folder-in-c As mentioned in the answer there's always the registry as well although it is a bit more complicated than just reading and writing a file. Still, with a couple of helper functions that you write once it's not terrible. We still use ini files just because we wrote something a long time ago that still works for us. – Retired Ninja Oct 03 '22 at 16:15
1 Answers
2
Saving configuration data is handled differently in different operating systems:
on Unix systems, it is customary to use a text file in the home directory of the user with the values of the configuration settings, in a readable format, allowing for comments, and preferably keeping them across updates.
on Windows, albeit the same method works fine, there is a facility called the registry with various APIs to save and restore settings. For portability and usability, it is probably simpler to use a configuration file on legacy systems too.