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
5
  • 2
    What effort did you make to achieve it? Jan 3, 2012 at 21:44
  • Possible duplicate: stackoverflow.com/q/12633/10077 Jan 3, 2012 at 21:46
  • Dear Paul,I trying hardly before I ask such question I try to use (GetPrivateProfileInt) but it didn't work with me.I am just junior in c++ the error is (GetPrivateProfileInt identifier not found Jan 3, 2012 at 22:06
  • I don't see any point of downvoting.
    – CDT
    Jun 8, 2013 at 11:41
  • I know its too late but from the OP's comment above, I think the problem with him is not including "Windows.h" or linking the correct library. Use #include<windows.h> and #pragma comment(lib, 'kernel32.lib')
    – Abhineet
    Jul 10, 2013 at 7:02

3 Answers 3

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

1
  • i saw no explicit mention of windows as the platform in use? good advice as a replacement for storing configuration on windows though. Oct 23, 2019 at 23:19
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.

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

2
  • 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 3, 2012 at 4: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. Oct 23, 2019 at 23:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.