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 KbDownload source project - 33 Kb

Comments
There are no comments yet. Be the first to comment!