views:

10604

answers:

9

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?

+8  A: 

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

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

Lars Mæhlum
+3  A: 

Just use the Win32 APIs. Don't worry, they're easy as pie.

Stu
+50  A: 

You can use the Windows API functions, such as GetPrivateProfileString() and GetPrivateProfileInt().

Joel Spolsky
+2  A: 

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.

crashmstr
+19  A: 

If you need a cross-platform solution, try Boost's Program Options library.

Adam Mitz
i would suggest this library too
varnie
this is the way to go, I don't understand why people just up-vote not-so-general answer.
Gollum
+3  A: 

I use SimpleIni. It's cross-platform.

Harold Ekstrom
+3  A: 

Have you tried libconfig; very JSON-like syntax. I prefer it over XML configuration files.

widgisoft
A: 

google sscanf or fscanf

+2  A: 

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.

It is very easy to read with simpleIni. Here is an example:

#include "SimpleIni\SimpleIni.h"    
CSimpleIniA ini;
ini.SetUnicode();
ini.LoadFile(FileName);
const char * pVal = ini.GetValue(section, entry, DefaultStr);
Mike