C语言 在 Linux 上使用 C 语言中的 INI 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2280352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use an INI file in C on Linux
提问by Kangkan
Is there a standard way of reading a kind of configuration like INIfiles for Linux using C?
是否有一种标准方法可以使用 C读取诸如适用于 Linux 的INI文件之类的配置?
I am working on a Linux based handheld and writing code in C.
我正在开发基于 Linux 的手持设备并用 C 编写代码。
Otherwise, I shall like to know about any alternatives.
否则,我想知道任何替代方案。
Final update:
最终更新:
I have explored and even used LibConfig. But the footprint is high and my usage is too simple. So, to reduce the footprint, I have rolled out my own implementation. The implementation is not too generic, in fact quite coupled as of now. The configuration file is parsed once at the time of starting the application and set to some global variables.
我已经探索过甚至使用过 LibConfig。但是占用空间大,我的用法也太简单了。因此,为了减少占用空间,我推出了自己的实现。实现不是太通用,实际上到目前为止已经很耦合了。配置文件在启动应用程序时解析一次,并设置为一些全局变量。
采纳答案by shuvalov
Try libconfig:
尝试libconfig:
a simple library for processing structured configuration files, like this one: test.cfg. This file format is more compact and more readable than XML. And unlike XML, it is type-aware, so it is not necessary to do string parsing in application code.
Libconfigis very compact — a fraction of the size of the expat XML parser library. This makes it well-suited for memory-constrained systems like handheld devices.
The library includes bindings for both the C and C++ languages. It works on POSIX-compliant UNIX and UNIX-like systems (GNU/Linux, Mac OS X, Solaris, FreeBSD), Android, and Windows (2000, XP and later)...
一个用于处理结构化配置文件的简单库,例如:test.cfg。这种文件格式比 XML 更紧凑、更易读。与 XML 不同的是,它是类型感知的,因此不需要在应用程序代码中进行字符串解析。
Libconfig非常紧凑 — 只是 expat XML 解析器库大小的一小部分。这使得它非常适合手持设备等内存受限的系统。
该库包括 C 和 C++ 语言的绑定。它适用于符合 POSIX 的 UNIX 和类 UNIX 系统(GNU/Linux、Mac OS X、Solaris、FreeBSD)、Android 和 Windows(2000、XP 及更高版本)...
回答by gnud
No, there isn't one standard way. I'm sorry, but that is probably the most precise answer :)
不,没有一种标准的方式。对不起,但这可能是最准确的答案:)
You could look at this list of Linux configuration file libraries, though. That might be helpful.
不过,您可以查看此Linux 配置文件库列表。这可能会有所帮助。
回答by unwind
If you can use the (excellent, in any C-based application) glib, it has a key-value file parserthat is suitable for .ini-style files. Of course, you'd also get access to the various (very nice) data structures in glib, "for free".
如果您可以使用(非常好,在任何基于 C 的应用程序中)glib,它就有一个适用于 .ini 样式文件的键值文件解析器。当然,您还可以“免费”访问 glib 中的各种(非常好的)数据结构。
回答by Tim Post
There is an updated fork of iniparser at ccan, the original author has not been able to give it much attention over the years. Disclaimer - I maintain it.
在ccan有一个 iniparser 的更新分支,原作者多年来一直未能给予太多关注。免责声明 - 我维护它。
Additionally, iniparser contains a dictionary that is very useful on its own.
此外,iniparser 包含一个本身就非常有用的字典。
回答by Bernardo Ramos
If you need a fast and small code just for reading config files I suggest the inih
如果您需要一个快速而小巧的代码来读取配置文件,我建议使用inih
It loads the config file content just once, parse the content and calls a callback function for each key/value pair.
它只加载一次配置文件内容,解析内容并为每个键/值对调用回调函数。
Really small. It can be used on embedded systems too.
真的很小。它也可以用于嵌入式系统。
回答by David
I hate to suggest something entirely different in suggesting XML, but libexpat is pretty minimal, but does XML.
我讨厌在建议 XML 时提出完全不同的建议,但 libexpat 非常小,但 XML 确实如此。
I came to this conclusion as I had the same question as you did, but then I realized the project already had libexpat linked-in--and I should probably just use that.
我得出这个结论是因为我和你有同样的问题,但后来我意识到该项目已经链接了 libexpat——我可能应该使用它。

