c++ win32如何存储持久数据
我正在开发一个 C++ win32 应用程序,我想在其中实现持久存储。我知道我可以使用文件系统、数据库或注册表,但是存储的最佳方法是什么:
boolean
unsigned int
wchar_t [256]
boolean [15]
仅当应用程序启动和关闭时,我才需要从存储中读取/写入。
这是一个用户级应用程序,数据应该按用户存储。我想存储应用程序首选项(轨迹栏位置、一些设置、运行次数等),因此无需导入/导出设置和故障排除。
哪种存储方法最适合此类数据?
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 application 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的解决方案是将信息存储在 .ini 文件 在用户的 APPDATA 路径。
The simplest solution is to store the information in a .ini file under the user's APPDATA path.
仅考虑序列化/protobuffer:http://code.google.com/p/protobuf/
您在其中写入应用程序的某些用户特定路径
Consider just serialization/protobuffer: http://code.google.com/p/protobuf/
where you write to some user specific path for the application
我认为在 Windows 上编写设置的最简单方法是将它们放入注册表中。对于用户特定的设置,您将在 HKEY_CURRENT_USER 部分中写入。 这里是完整的Win32 API 中的注册表访问函数列表。
如果您认为需要将应用程序移植到其他平台,那么您可能需要考虑更跨平台友好的解决方案。我在应用程序中所做的是以 Unix 方式编写设置,并在用户的主目录中设置一个设置文件。这样做的另一个好处是,您可以使用基于文本的格式,您(或您的用户)可以根据需要对其进行编辑,例如,您可以使用 WritePrivateProfileString 或类似的 API 调用。请注意,在 Windows 上,应用程序不会直接在主目录中写入设置文件,这些文件通常存放在“AppData”用户特定文件夹中。您可以通过调用 SHGetFolderPath 函数。
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.