开发者

Save user settings in C

I am looking开发者_JAVA百科 for a way to save user preferences in C. Something like the Java Preferences API would be very good.


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.


On Windows one of the simple options is to use Get/WritePrivateProfileString functions to operate on standard INI settings file.


If you're using the Qt application framework, then QSettings is the equivalent class to Java's Preferences. Qt works on Linux, Mac, and Windows. If you're not currently using a C++ application framework, then Qt is good because it provides a lot of useful functions and the SIGNAL/SLOT mechanism is handy to boot. If you already have an application framework, I wouldn't recommend adding Qt just to save your preferences, because you'll need to run your program as a QCoreApplication.

Qt download: http://qt-project.org/downloads

QtSettings Class Reference: http://qt-project.org/doc/qt-4.8/qsettings.html

Related StackOverflow question: Store settings of qt application using QSettings

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜