有一些常见的方法来写入和读取配置文件吗?

发布于 2024-08-29 08:19:59 字数 94 浏览 4 评论 0原文

我需要我的程序创建和编辑一个配置文件,其中包含有关对象集的信息,然后在每次执行时读取它。我可以使用某种配置样式指南吗?

我在 Windows 上使用 C++。

I need my program to create and edit a config file, which would contain information about set of objects, and than read it at every execution. Is there some sort of guideline for config style that i can use?

I'm using C++ on windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

攀登最高峰 2024-09-05 08:19:59

我建议查看 boost::property_tree

属性树库提供了一种数据结构,用于存储任意深度嵌套的值树,并通过某个键在每个级别进行索引。树的每个节点都存储自己的值,以及其子节点及其键的有序列表。该树允许通过路径轻松访问其任何节点,路径是多个键的串联。

此外,它还包含 XML、INI 和 JSON 的解析器和生成器,因此您可以以您选择的格式保存/加载。

I recommend checking out boost::property_tree.

The Property Tree library provides a data structure that stores an arbitrarily deeply nested tree of values, indexed at each level by some key. Each node of the tree stores its own value, plus an ordered list of its subnodes and their keys. The tree allows easy access to any of its nodes by means of a path, which is a concatenation of multiple keys.

Additionally, it contains parsers and generators for XML, INI, and JSON, so you can save/load in the format of your choice.

掩于岁月 2024-09-05 08:19:59

这很大程度上取决于语言、平台和配置文件的范围。例如,Java 世界中有用于配置的 properties 文件,以及此处已经提到的其他文件,例如 YAML 。

XML 通常不适合用于配置,因为它非常冗长。您仍然可以在许多应用程序、Web 框架等中找到它。

我认为最佳实践是为手头的工作选择正确的配置格式。您可以自己评估和尝试它们,同时考虑以下几点:

  1. 标准是什么? (例如 Windows 中的 ini 文件、Java 中的属性文件)
  2. 我的语言是否有本机支持,或者我必须推出自己的实现吗?
  3. 我的配置格式可以轻松描述我想要存储为配置的内容吗?

我相信您可以考虑其他考虑因素。如果您更新问题以澄清范围,您将获得更有用的答案。

It largely depends on the language, platform and the scope of your config files. There's for example the properties files in Java world for configuration, and others already mentioned here such as YAML.

XML is generally frowned upon for configuration, since it's very verbose. You still find it in many applications, Web frameworks, etc.

I think the best practice would be to choose the right configuration format for the job at hand. You can evaluate and try them for yourself, while considering these pointers:

  1. What's the standard? (e.g. ini files in Windows, properties files in Java)
  2. Is there native support in my language, or do I have to roll my own implementation?
  3. Can my configuration format easily describe what I want to store as configuration?

I'm sure you could think of other considerations. If you update your question to clarify the scope, you'll get more useful answers.

江南月 2024-09-05 08:19:59

YAML 是一种非常流行的创建配置文件的解决方案。例如,Ruby on Rails 和 Google AppEngine 就主要使用它。 YAML 库适用于许多(如果不是大多数)语言。

YAML is a very popular solution for creating configuration files. It is used prominently by Ruby on Rails and Google AppEngine, for instance. YAML libraries are available for many if not most languages.

最冷一天 2024-09-05 08:19:59

如果您正在使用 Qt(根据您的其他问题),

请查看 QSettings 类。我一直在使用它们来设置许多持久信息,例如上次访问的目录以及上次使用的选项。

If you're working with Qt (based on your other questions),

look at the QSettings class. I've been using these to set a lot of persistent info, such as last visited directory, and which options were last used.

暗地喜欢 2024-09-05 08:19:59

boost::program_options 具有一定的读取ini 文件的能力。

好处是允许统一读取 ini 文件和命令行选项。随着 Windows 程序的发展,这可能不会被视为一个有趣的未来,但对于我的 UNIX 应用程序来说,这是一个杀手级功能。

然而,boost::po 不允许编辑或写入 ini 文件,因此使用 boost::property_tree 可能会更好。

boost::program_options has some capacity to read ini files.

What is nice is that is allow to unify reading ini file and command line options. As Windows program goes, that may not be seen as such an interresting future, but for my unix app it was a killer feature.

However, boost::po does not allow editing or writing of ini files, hence you'll probably be better off with boost::property_tree.

牵你手 2024-09-05 08:19:59

最常见的格式是 INI 文件 格式。大多数 mdoern 语言应该内置一些东西来处理这些问题,否则我确信您能够找到第三方库。

如果做不到这一点,您可以使用 XML,如果您的语言支持它,则可以序列化到这些 XML 文件或从这些 XML 文件反序列化。如果您不想/不能使用 XML,大多数语言都会有它们使用的默认格式,例如对象中数据的纯二进制转储。

The most common format is the INI File format. Most mdoern languages should have something built in to handle these, otherwise I'm sure you'd be able to find a third-party library.

Failing this, you could use XML, and if your language supports it, serializing to and deserializing from these XML files. If you don't want to/can't use XML, most languages will have a default format they use, such as a pure binary dump of the data in an object.

独自←快乐 2024-09-05 08:19:59

有一个免费的跨平台配置文件管理库,称为 libconfig。它支持非常好的基于Yaml的语法,也支持用户定义的语法。它还具有许多其他计算语言绑定。查看它的主页: http://www.hyperrealm.com/libconfig/

There is a free, cross-platform library for configuration file management, called libconfig. It supports very nice Yaml-based grammar, and supports user-defined grammar too. It also has many other computing language bindings. See it's homepage: http://www.hyperrealm.com/libconfig/ .

束缚m 2024-09-05 08:19:59

在 Java 世界中,属性文件非常容易使用:
http://java.sun.com/ j2se/1.5.0/docs/api/java/util/Properties.html
Properties 类代表一组持久的属性。属性可以保存到流中或从流中加载。属性列表中的每个键及其对应的值都是一个字符串。

正如其他人所说,您需要描述您的环境才能获得最佳推荐。

In the Java world, a properties file is pretty easy to use:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

As the others have said, you would need to describe your environment to get the best recommendation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文