Visual C++: Read and Write INI Files

I find it useful to keep the settings for my application separated from the code in a initialization (INI) file. The application code reads these settings at runtime and uses them for initialization of its objects. This separation means that I can try the application with different settings without having to recompile the code. (C++ code takes ages to compile!) And the INI file is human readable and writable easily using any text editor.

A simple INI file:

;------------------------------------------------
; Foobar Settings (foobar.ini)
;------------------------------------------------

; Database settings
[DB_SETTINGS]
USER_NUM_MAX = 256  ; Maximum number of users

; Operation settings
[OP_SETTINGS]
CRITICAL_SIZE = 100000  ; Maximum memory
;------------------------------------------------

The Initialization (INI) file format is simple. Anything to the right of a semicolon ; is a comment. Sections are named within a pair of square brackets []. Key-value pairs are written as key=value.

To read an integer or string value use GetPrivateProfileInt() or GetPrivateProfileString(). To write a string value back to a key use WritePrivateProfileString(). (There is no function to write back an integer, convert it to a string.)

#include <Windows.h>

int userNumMax = GetPrivateProfileInt("DB_SETTINGS", "USER_NUM_MAX", 0, "foobar.ini");
WritePrivateProfileString("DB_SETTINGS", "USER_NUM_MAX", "99", "foobar.ini");

For other functions related to INI files look for those with the prefix GetPrivateProfile and WritePrivateProfile in Registry Functions. The functions with prefix GetProfile are meant for win.ini, which should be not be useful to anyone!

5 thoughts on “Visual C++: Read and Write INI Files

  1. Hey!

    I wish it could be that easy as you wrote it! To stay at your example, “DB_SETTINGS” is a System::String != LPCTSTR so this piece of code will never work in VC++. If it does to you then tell me how this miracle looks like…

    • NotTellingYou: The conversion shown above is from literal string to LPCTSTR, it should work. If you are converting from a string, then use the c_str() method of the string to get its underlying character array.

  2. I want to create an ini file through coding. If on application path ini file is not found then application itself create the ini file with the section and the name. And after creation the default value is filled from the code. Is this possible in embedded visual c++? If yes the tell me and help me to solve this problem. My basic condition is that user never create ini file and never change the field of that ini file.Do evething from the code. Please help me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s