I am looking for a way to save user preferences in C. Something like the Java Preferences API would be very good.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well, to do this in a portable way in C or C++, you’d need to define an interface. In C this would be a preference module of some sort. In C++, this would be an abstract class. You would then program to this interface when you want to load and save preferences. The point is that as a user of the interface, you shouldn’t care where or how they are stored, as long as you can load and save them.
You would then write the interface code for the OS you want to support. For example: on Windows, you’d write methods that read from the registry and write to it. On Mac, you’d read and write from and to plist files.
Hope this helps. Oh yes, C and C++ do not come with built in support for preference saving and loading. You will have to write code to do it.
Finally, you can read from and write to text files, binary files or any other files for that matter. However, unless you have a good reason not to, you should use the standard OS system for preferences. This will make things easier down the line when the way you use your preferences changes.