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
The Secret master the mizuno-arena Is Fairly Straight forward!
Posted by Acuddence on 04/30/2013 07:39pmBeginner queries about nike clarified not to mention reasons why you really need to check out each and every word within this report.[url=http://www.nikejpgolf.biz/]nike ã´ã«ã[/url] Your primary double strain on nike [url=http://www.nikejpgolf.biz/nike-ã´ã«ããã¼ã«-c-23.html]ãã¤ã ãã¼ã«[/url] Progressive questions on nike have been answered and the reason why you have to take a look at every single message within this guide. [url=http://www.nikejpgolf.biz/nike-ã¢ã¤ã¢ã³-c-1.html]ãã¤ãã¯ã©ã[/url] Unprejudiced report shares 2 innovative new things over nike that absolutely no one is speaking about. [url=http://www.nikejpgolf.biz/nike-ã¢ã¤ã¢ã³-c-1.html]ãã¤ã[/url] All the nike Sector Talk - Visitors who cares is announced as the winner?!? [url=http://www.nikejpgolf.biz/nike-ã´ã«ãã·ã¥ã¼ãº-c-15.html]nike air jordan[/url] Supplies and performance throughout Rhode Island : nike has left without any see you later [url=http://www.nikeyasuyi.com/]nike[/url] Resources and fabrication in Sin City - mizuno leaves with no see you later [url=http://www.nikeyasuyi.com/nikeãã¤ãRunning-c-3.html]nike ã©ã³ãã³ã°[/url] A nike Small business Talk : Users who cares for virtually nothing triumphs? [url=http://www.nikeyasuyi.com/nikeãã¤ãDunk-c-9.html]ãã¤ã·ã¥ã¼ãº[/url] Their nike Market Dialogue - - And so, who loves next to nothing triumphs?? [url=http://www.nikeyasuyi.com/nikeãã¤ãDunk-c-9.html]ãã¤ã·ã¥ã¼ãº[/url] nike brings brand-new life to an old dilemma- platinum quality
ReplyCreation of Ini file
Posted by Vaibhav on 03/28/2012 06:36amI 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.
Replyini file and WDM
Posted by khkh on 07/20/2005 07:56amHi, I Want to know how to save setting of WDM to ini file, Thanks.
ReplyFine
Posted by sangamesha on 10/19/2004 07:09ami need to create a .ini file
Posted by Legacy on 02/21/2004 12:00amOriginally 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
ReplyStupid Article!
Posted by Legacy on 08/23/2003 12:00amOriginally posted by: Gool
Stupid Article!s
Reply
How to i create ini file.
Posted by Legacy on 05/06/2003 12:00amOriginally posted by: no_body69
How to i create ini file.
Replyplease give me code (VC++).
Thank!
Posted by Legacy on 03/13/2003 12:00amOriginally posted by: kerb
Thanks a lot. it has been very helpful.
ReplyNot working on my Win98 too :(
Posted by Legacy on 01/12/2003 12:00amOriginally posted by: Usman
Not working on my Win98 too :(
ReplyDidn't work for me
Posted by Legacy on 01/10/2003 12:00amOriginally posted by: mika
I tried to read a relativly large ini file with around 100 sections, each section had around 7000 entries,
nice work by you aisha but it sucked. Seems like it took forever to read that file
Reply