4

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++?

Could you please help provide me very simple code to read simple file?

E.G.:

[SECTION]
key1=int_value1
key2=string_value2
Community
  • 1
  • 1
Khadijah J Shtayat
  • 190
  • 1
  • 3
  • 10

3 Answers3

6

https://github.com/brofield/simpleini is 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.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
dweeves
  • 5,525
  • 22
  • 28
6

Look into the GetPrivateProfileString and WritePrivateProfileString APIs:

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

Mario
  • 393
  • 2
  • 5
  • i saw no explicit mention of windows as the platform in use? good advice as a replacement for storing configuration on windows though. – braindigitalis Oct 23 '19 at 23:19
-6

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).

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).

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.

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).

ssube
  • 47,010
  • 7
  • 103
  • 140
  • 12
    telling someone to avoid using something because it's 'old' is almost never good advice. If that's really the best reason for not using it, then there's nothing wrong with using it (and I have no idea what you mean by API's for them being "weak"). – Fred Dec 03 '12 at 04:45
  • 1
    In response to @Fred - on windows, built in win32 ini reading/writing functions are discouraged and depreciated, since windows 95 where the registry took their place. On other platforms, or on windows with a third party library it's of course still an OK idea to use them as easily user-editable configuration files where all you need is key-value stores, and json or xml would just be overkill. Nobody likes to hand-edit XML. – braindigitalis Oct 23 '19 at 23:16