在 C++ 中解析 INI 文件的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12633/
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
What is the easiest way to parse an INI File in C++?
提问by conmulligan
I'm trying to parse an INI file using C++. Any tips on what is the best way to achieve this? Should I use the Windows API tools for INI file processing (with which I am totally unfamiliar), an open-source solution or attempt to parse it manually?
我正在尝试使用 C++ 解析 INI 文件。关于什么是实现这一目标的最佳方法的任何提示?我应该使用 Windows API 工具来处理 INI 文件(我完全不熟悉它)、开源解决方案还是尝试手动解析它?
回答by Adam Mitz
If you need a cross-platform solution, try Boost's Program Optionslibrary.
如果您需要跨平台解决方案,请尝试使用 Boost 的程序选项库。
回答by Joel Spolsky
You can use the Windows API functions, such as GetPrivateProfileString()and GetPrivateProfileInt().
您可以使用 Windows API 函数,例如GetPrivateProfileString()和GetPrivateProfileInt()。
回答by Lars M?hlum
I have never parsed ini files, so I can't be too specific on this issue.
But i have one advice:
Don't reinvent the wheel as long as an existing one meets your requirements
我从来没有解析过 ini 文件,所以我不能在这个问题上太具体。
但我有一个建议:只要现有的轮子满足您的要求,就
不要重新发明轮子
http://en.wikipedia.org/wiki/INI_file#Accessing_INI_files
http://sdl-cfg.sourceforge.net/
http://sourceforge.net/projects/libini/
http://www.codeproject.com/KB/files/config-file-parser.aspx
http://en.wikipedia.org/wiki/INI_file#Accessing_INI_files
http://sdl-cfg.sourceforge.net/
http://sourceforge.net/projects/libini/
http://www.codeproject.com/KB /files/config-file-parser.aspx
Good luck :)
祝你好运 :)
回答by Dat Chu
If you are already using Qt
如果你已经在使用 Qt
QSettings my_settings("filename.ini", QSettings::IniFormat);
Then read a value
然后读取一个值
my_settings.value("GroupName/ValueName", <<DEFAULT_VAL>>).toInt()
There are a bunch of other converter that convert your INI values into both standard types and Qt types. See Qt documentation on QSettings for more information.
还有很多其他转换器可以将您的 INI 值转换为标准类型和 Qt 类型。有关更多信息,请参阅有关 QSettings 的 Qt 文档。
回答by Mike
this question is a bit old, but I will post my answer. I have tested various INI classes (you can see them on my website) and I also use simpleIni because I want to work with INI files on both windows and winCE. Window's GetPrivateProfileString() works only with the registry on winCE.
这个问题有点老了,但我会发布我的答案。我已经测试了各种 INI 类(您可以在我的网站上看到它们)并且我也使用 simpleIni,因为我想在 windows 和 winCE 上使用 INI 文件。Window 的 GetPrivateProfileString() 仅适用于 winCE 上的注册表。
It is very easy to read with simpleIni. Here is an example:
使用 simpleIni 很容易阅读。下面是一个例子:
#include "SimpleIni\SimpleIni.h"
CSimpleIniA ini;
ini.SetUnicode();
ini.LoadFile(FileName);
const char * pVal = ini.GetValue(section, entry, DefaultStr);
回答by nimcap
inihis a simple ini parser written in C, it comes with a C++ wrapper too. Example usage:
inih是一个用 C 编写的简单的 ini 解析器,它也带有一个 C++ 包装器。用法示例:
#include "INIReader.h"
INIReader reader("test.ini");
std::cout << "version="
<< reader.GetInteger("protocol", "version", -1) << ", name="
<< reader.Get("user", "name", "UNKNOWN") << ", active="
<< reader.GetBoolean("user", "active", true) << "\n";
The author has also a list of existing libraries here.
作者还在这里列出了现有库。
回答by Christopher Lightfoot
Have you tried libconfig; very JSON-like syntax. I prefer it over XML configuration files.
你试过libconfig 吗?非常类似 JSON 的语法。我更喜欢它而不是 XML 配置文件。
回答by gast128
If you are interested in platform portability, you can also try Boost.PropertyTree. It supports ini as persistancy format, though the property tree my be 1 level deep only.
如果您对平台可移植性感兴趣,您也可以尝试 Boost.PropertyTree。它支持 ini 作为持久性格式,尽管属性树可能只有 1 级深。
回答by crashmstr
Unless you plan on making the app cross-platform, using the Windows API calls would be the best way to go. Just ignore the note in the API documentation about being provided only for 16-bit app compatibility.
除非您打算使应用程序跨平台,否则使用 Windows API 调用将是最好的方法。只需忽略 API 文档中关于仅为 16 位应用程序兼容性提供的注释。