How to read/write an INI file

30 12 2009



INI files are plain text files that contain configuration information. Each INI files contains one or more sections and each sections contain zero or more entries. How shall we read an entry from a section or write to a section.



Read and write operations of an initialization file can be performed bu using the following APIs  GetPrivateProfileString and WritePrivateProfileString respectively.



Read from INI file

void ReadFromIniFile()
   TCHAR szKeyValue[32];
   GetPrivateProfileString(_T("Section 1"), _T("Key 1"), _T("") ,szKeyValue, 32, _T("C:\\test.ini"));


Write to INI file

void WriteToIniFile()
  WritePrivateProfileString(_T("Section 2"), _T("Key 2"), _T("Hello World"), _T("C:\\test.ini"));



Integer value associated with a key in the specified section of an INI file can be retrieved by using the  API GetPrivateProfileInt.