c++ win32 how to store persistent data
I'm developing a c++ win32 application and i would like to implement persistent storage into it. I know I can use filesystem or a database or the registry, but what would be the best way for storing a:
boolean
unsigned int
wchar_t [256]
boolean [15]
I need to read/write from the storage only when the applicat开发者_开发知识库ion starts and shuts down.
This is a user level application and the data should be stored per user. I want to store application preferences (trackbar position, some settings, number of runs,..) so there is no need to import/export settings and troubleshooting.
Which storage method would be the most appropriate for this type of data?
I think the easiest way to write settings on Windows is to put them in the registry. For user specific settings you will write in the HKEY_CURRENT_USER section. Here is the full list of registry access functions in the Win32 API.
If you consider you will ever need to port your application to other platforms, then you may want to look at a more cross-platform friendly solution. What I do in my apps is to write settings the Unix way, with a settings file in the user's home directory. This has the added benefit that you can use a text based format that you (or your users) can edit if necessary, for example, you could write it in the venerable .ini format using the WritePrivateProfileString or similar API calls. Note that on Windows, applications don't write settings files directly in the home directory, there is the "AppData" user specific folder where these files typically go. You can obtain the path of this folder by calling the SHGetFolderPath function.
The simplest solution is to store the information in a .ini file under the user's APPDATA path.
Consider just serialization/protobuffer: http://code.google.com/p/protobuf/ where you write to some user specific path for the application
精彩评论