How to Read and Write an INI File

Environment: Visual C++ 6, Win32, MFC, Win XP, Win 2000

Introduction

CIniReader is a class that reads and writes to and from an INI file. Here is a functions list of this class:

Function Description
setINIFileName Sets the INI File name to read and write.
getKeyValue Retrieves a key value given the section and key name.
getSectionData Retrieves all key/value pairs of a given section.
getSectionNames Retrieves all the sections in an INI file.
sectionExists Finds out whether a given section exists.
setKey Modifies the key value or to create a key value pair for the specified section.

The functions in CIniReader use GetPrivateProfilexxx () functions to set and get information stored in an INI file. By having idea of the SDK functions such as GetPrivateProfilexxx (), the functionality of the class can easily be extended.

Here is the CIniReader header file:

//
// CIniReader- Header File
//
class CIniReader
{
public:
  // method to set INI file name, if not already specified
  void setINIFileName(CString strINIFile);

  // methods to return the lists of section data and section names
  CStringList* getSectionData(CString strSection);
  CStringList* getSectionNames();

  // check whether the section exists in the file
  BOOL sectionExists(CString strSection);

  // updates the key value, if key already exists,
  // else creates a key-value

pair
  long setKey(CString strValue, CString strKey,
              CString strSection);

  // give the key value for the specified key of a section
  CString getKeyValue(CString strKey,CString strSection);

  // default constructor
  CIniReader()
  {
    m_sectionList = new CStringList();
    m_sectionDataList = new CStringList();
  }

  CIniReader(CString strFile)
  {
    m_strFileName = strFile;
    m_sectionList = new CStringList();
    m_sectionDataList = new CStringList();
  }

  ~CIniReader()
  {
    delete m_sectionList;
    delete m_sectionDataList;
  }

private:
  // lists to keep sections and section data
  CStringList *m_sectionDataList;
  CStringList *m_sectionList;

  CString m_strSection;
  long m_lRetValue;

  // ini file name
  CString m_strFileName;
};

How to Use the Class

The demo project shows how to use the class. It takes from the user the fully qualified path and name of the INI file as "C:\WINDOWS\ODBC.INI" in the INI File name edit box. List Sections fills the list of sections in the INI file. Specifying any of the section names and clicking the List Keys buttons will fill the second list box with the section data (all key and value pairs). Specifying a key name and pressing the Show Key Value button will display the value of the key. Update Key Value will update the modified value from the key value edit box for the key. If the key is not already present, the update key value will create the specified key-value pair in the INI file. Be sure about what you are doing because it will modify the keys and values of the INI file.

Currently, the buffer limit is up to 2000 characters; this can be increased according to the requirement.

After setting the INI file name just as the following code shows, you can proceed with CIniReader functions:

// example of using the setINIFileName method
CIniReader  m_IniReader;
m_strINIFile = C:\\WINDOWS\\ODBC.INI;
m_IniReader.setINIFileName (m_strINIFile); 

That's it. The class is so simple and easy to use, I am sure you will have no problem.

About the Author

Aisha has an M.Sc. in Computer Science with a top position in her university. She has worked mainly in VC++ 6, MFC, ATL, COM/DCOM, ActiveX, C++, ASP, VB, SQL, and so forth. These days, she is working on the dot-Net framework, managed C++, and C#. Inspired with nature, she loves to seek knowledge. She is so fond of travelling that she don't want to die without seeing this whole world.

Website: http://aisha.hostmb.com
E-mail: ais_ikr@yahoo.com

If there are any suggestions or comments, you are most welcome to contact me. My Web site is http://aisha.hostmb.com.

Downloads

Download demo project - 2 Kb
Download source project - 33 Kb


Comments

  • Syntax for allowing an installed program by default

    Posted by Sailesh Saseendran on 08/01/2016 08:14am

    Hi, I just want to configure Patriot NG in such a way that it should not ask for permission to execute installed programs. It has a ownrules.ini file so I want to edit it. so will you please tell me the syntax for exceptions or silently allowing the program without a echo or popup. I know to add monitoring ports in other files. But not getting this straight. If anybody can help me on the syntax I can do the rest. Thanks in advance.

    Reply
  • Please Help Me Convert From C# To VB.Net

    Posted by James Frimpong on 08/24/2015 03:42pm

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { try { var XML = "XML=\n" + "\n" + "\n" + "SEND\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "SMS\n" + "Test message [UTF-8]\n" + "\n" + "\n" + "380972920000\n" + "\n" + "\n"; HttpWebRequest request = WebRequest.Create("http://atompark.com/members/sms/xml.php") as HttpWebRequest; request.Method = "Post"; request.ContentType = "application/x-www-form-urlencoded"; ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(XML); request.ContentLength = data.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(data, 0, data.Length); using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) throw new Exception(String.Format( "Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription)); StreamReader reader = new StreamReader(response.GetResponseStream()); Console.WriteLine(reader.ReadToEnd()); Console.ReadKey(); } } catch (Exception e) { Console.WriteLine(e.Message); Console.ReadKey(); } } } }

    Reply
  • Creation of Ini file

    Posted by Vaibhav on 03/28/2012 06:36am

    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.

    Reply
  • ini file and WDM

    Posted by khkh on 07/20/2005 07:56am

    Hi, I Want to know how to save setting of WDM to ini file, Thanks.

    Reply
  • Fine

    Posted by sangamesha on 10/19/2004 07:09am

    It is good

    Reply
  • i need to create a .ini file

    Posted by Legacy on 02/21/2004 12:00am

    Originally posted by: Erick

    Hello, that class of yours us very nice but i have a problem because what if your app nedd to create that ini file and after that it has to read and write things?, i dont know how to do that (create a .ini file by code), i hope somebody here can help me

    Thanks

    Erick

    Reply
  • Stupid Article!

    Posted by Legacy on 08/23/2003 12:00am

    Originally posted by: Gool

    Stupid Article!s

    Reply
  • How to i create ini file.

    Posted by Legacy on 05/06/2003 12:00am

    Originally posted by: no_body69

    How to i create ini file.
    please give me code (VC++).

    Reply
  • Thank!

    Posted by Legacy on 03/13/2003 12:00am

    Originally posted by: kerb

    Thanks a lot. it has been very helpful.

    Reply
  • Not working on my Win98 too :(

    Posted by Legacy on 01/12/2003 12:00am

    Originally posted by: Usman

    Not working on my Win98 too :(

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date