如何在 C++ 中读取 ini 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8718991/
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
How can I read an ini file in C++?
提问by Khadijah J Shtayat
Possible Duplicate:
What is the easiest way to parse an INI File in C++?
How can I read the section,key and value from .ini file in C++?
如何从 C++ 中的 .ini 文件中读取部分、键和值?
Could you please help provide me very simple code to read simple file?
你能帮我提供非常简单的代码来读取简单的文件吗?
E.G.:
例如:
[SECTION] key1=int_value1 key2=string_value2
回答by Mario
Look into the GetPrivateProfileString and WritePrivateProfileString APIs:
查看 GetPrivateProfileString 和 WritePrivateProfileString API:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724353%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724353%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms725501%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms725501%28v=vs.85%29.aspx
回答by dweeves
https://github.com/brofield/simpleiniis the one i use to read ini files, it's a simple template class that is easy to use and integrate , as a bonus , it is cross platform.
https://github.com/brofield/simpleini是我用来读取ini文件的,它是一个简单的模板类,易于使用和集成,作为奖励,它是跨平台的。
回答by ssube
For starters, INI files are obsolete and should be avoided when you can. They seem tempting because of the simple format (perhaps rightly so), but are limited and APIs for accessing them are deprecated and/or weak. If you can, using a simple XML config file may be better (although it may also require external code and many XML parsers are fat and ugly; pugixml is my favorite).
首先,INI 文件已经过时,应该尽可能避免使用。由于格式简单,它们看起来很诱人(也许是正确的),但它们是有限的,并且用于访问它们的 API 已被弃用和/或很弱。如果可以,使用简单的 XML 配置文件可能会更好(尽管它可能还需要外部代码,而且许多 XML 解析器又胖又丑;pugixml 是我最喜欢的)。
If you do have to use an INI, you want the GetPrivateProfile*
functions, as listed here(I am assuming you're targeting Windows and can use WinAPI functions, since you're on VS).
如果您确实必须使用 INI,则需要此处GetPrivateProfile*
列出的函数(我假设您面向 Windows 并且可以使用 WinAPI 函数,因为您使用的是 VS)。
The precise behavior of the functions is actually somewhat complex, but for the most part they'll work as you expect, if awkwardly. You can query a file for sections, ints and strings, as:
函数的精确行为实际上有些复杂,但在大多数情况下,它们会按您的预期工作,如果笨拙的话。您可以查询文件中的节、整数和字符串,如下所示:
UINT value = GetPrivateProfileInt("Section", "Key", DEFAULT_VALUE, "program.ini");
and so forth. A potential point of confusion is the parameter name for sections (lpAppName
), which isn't accurate when using a single-app/multi-section INI.
等等。一个潜在的混淆点是部分 ( lpAppName
)的参数名称,这在使用单应用程序/多部分 INI 时不准确。
Note, however, that the INI functions are slow. They often load and read through the entire file looking for the part they need, and consecutive calls will reopen and reread much of the file. They are old and not optimized, so you should keep calls down. If you need speed, cache the values, read them in a single shot, or use something that will (like XML).
但是请注意,INI 函数很慢。他们经常加载并通读整个文件以寻找他们需要的部分,连续调用将重新打开并重新读取文件的大部分内容。它们很旧而且没有优化,所以你应该保持通话。如果您需要速度,请缓存这些值,一次性读取它们,或者使用一些可以(例如 XML)的东西。