Click here to Skip to main content
15,887,027 members
Articles / Desktop Programming / MFC
Article

How to read and write an INI File

Rate me:
Please Sign up or sign in to vote.
4.61/5 (35 votes)
17 Sep 20022 min read 359.6K   11.2K   95   51
List sections and section data (key-value pairs). Displays a key value and creates/modifies a key-value pair

Sample Image - INI.jpg

Introduction

CIniReader is a class that reads/writes from/to an INI file.

Details

Here are functions list of this class:

  • setINIFileName Sets the INI File name to read and write.
  • getKeyValue Used to retrieve a key value given the section and key name.
  • getSectionData Used to retrieve all key/value pairs of a given section.
  • getSectionNames Used to retrieve all the sections in an ini file
  • sectionExists Used to find out if a given section exists.
  • setKey Used to modify the key value or to create a key value pair for the specified section.

The functions in CIniReader use GetPrivateProfilexxx () functions to set, get information stored in an INI file. By having idea of the SDK functions like 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 if 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?

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 key not already present, update key value will create the specified key-value pair in the INI file. Be sure what you are doing, as it will modify the keys and values of INI.

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

After setting the INI file name just as the following code, you can proceed with using 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.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Architect DWP
    United Kingdom United Kingdom
    I am currently working as Lead Solution Architect for Azure, AWS and hybrid cloud. I have been using and defining reference architecture, blueprint and patterns across digital. working with different vendors like Microsoft, Amazon, Redhat, Palo Alto, Venafi, Atlassian, Oracle and Google.
    I have worked as an architect for many other renowned govt and private organziations in UK like NHS, DfE, Liaison, Cloudsprint etc.

    I was historically working as a Technical Lead. I worked mainly in microsoft technologies,Windows, .NET, C#, VB.NET, ASP.NET, VC++ 6, MFC, ATL, COM/DCOM, SQL Server.

    Comments and Discussions

     
    GeneralRe: Request to Author Pin
    Christian Graus22-Jan-03 16:03
    protectorChristian Graus22-Jan-03 16:03 
    GeneralRe: Request to Author Pin
    Anonymous22-Jan-03 16:04
    Anonymous22-Jan-03 16:04 
    GeneralRe: Request to Author Pin
    Christian Graus22-Jan-03 16:12
    protectorChristian Graus22-Jan-03 16:12 
    GeneralRe: Request to Author Pin
    Anonymous22-Jan-03 16:28
    Anonymous22-Jan-03 16:28 
    GeneralRe: Request to Author Pin
    Christian Graus22-Jan-03 16:41
    protectorChristian Graus22-Jan-03 16:41 
    GeneralRe: Request to Author Pin
    22-Jan-03 16:49
    suss22-Jan-03 16:49 
    GeneralRe: Request to Author Pin
    Christian Graus22-Jan-03 16:53
    protectorChristian Graus22-Jan-03 16:53 
    GeneralRe: Request to Author Pin
    Anonymous22-Jan-03 16:59
    Anonymous22-Jan-03 16:59 
    Are YouStupid?
    Summary:
    Opponents of the usability movement claim that it focuses on stupid users and that most users can easily overcome complexity. In reality, even smart users prefer pursuing their own goals to navigating idiosyncratic designs. As Web use grows, the price of ignoring usability will only increase.
    Usability opponents often complain that we user advocates are overly focused on stupid people. They commonly claim that:

    We select stupid users for usability studies; our findings do not apply to smarter users.
    Our recommendation to make simplicity a major design goal stems from our misguided view that all users are stupid. In reality, they claim, many users are capable of navigating complex sites.
    Some people are so stupid that technology is beyond their grasp; making websites easy enough for everyone to navigate is an unrealistic goal.
    I'll now address each of these claims, and then examine how attitudes about usability and user intelligence will impact business as the Internet population grows.
    "Test Users are Stupid"
    Typically, when project managers observe their design undergoing a usability test, their initial reaction is: Where did you find such stupid users?
    This is exactly what happened recently when I released a WAP usability study. The study concluded that using WAP (to access the Internet through mobile phones) is too difficult for most purposes. In response, a group of big WAP investors rejected the findings, issuing a press release that claimed that the study's outcome would have been different if we had tested experienced WAP users. Although this might be true, their response misses the point.

    First, it ignores the fact that users' initial experience with a new technology is crucial. People will never become experienced users unless they are capable of learning the technology in the first place.
    Second, our study's participants used WAP phones for a week, and we tested them both at the beginning and the end of the period. If a full week of use is insufficient to learn a user interface, we are surely not talking about a mainstream consumer technology.
    Usability lessons are not always easy to take. It is a painful experience to sit in the back room watching as a user clicks every button on the screen except the one button that "obviously" leads to the answer. The first time project members observe a usability study, they almost always lapse into denial about the true lessons of the experience.
    Until we bring in the second user. He or she typically has many of the same problems as the first user. Then the third user comes in, and again: many of the same problems. At this point, designers often start to soften to the users' plight. If not, the fourth or fifth user will do the trick.

    When people have problems using a design, it's not because they are stupid. It's because the design is too difficult.

    "Real Users Don't Mind Complex Design"
    Enthusiasts sometimes defend bleeding-edge technology and complex designs with the claim that users actually like sophisticated websites. Users, they assert, are smart enough to handle complicated design.
    These enthusiasts labor under a miscomprehension about the Web's fundamental nature. It is not a question of whether users are capable of overcoming complexity and learning an advanced user interface. It is a question of whether they are willing to do so.

    I have conducted many usability studies with users who had immense computer experience, great aptitude for technology, and high levels of IQ and education. These users are just like anybody else: they just want to get their work done. They have neither the desire nor the time to learn the idiosyncrasies of individual websites.

    If you have doubts, run a test with network system administrators or international investment analysts, for example. What you'll discover is that they face plenty of complicated problems in their own work and they don't want to devote brain cells to your website or its design. They want to get in, get out, and move on with their own tasks.

    Design complexity is a barrier for users. While they certainly might be capable of jumping the barrier, why should they? The Web is about freedom of movement. Anything that stands in the way of immediate task completion will negatively impact the user's experience.

    "Some People are Too Stupid to Serve"
    Some people are smarter than others. Most readers of this column probably belong to the top 10% of the population in terms of intelligence. From such a vantage point, it is easy to think of other people as being stupid. But perhaps it is more fair and more accurate (not to mention more productive) to assume that the other 90% of the population form the mainstream audience. Not that they are stupid.
    Nonetheless, it might be true that some people do not have enough intelligence to use sophisticated and advanced high-tech systems. But are they online? Not likely at this point.

    Even in the most wired societies like the United States and Scandinavia, only half the population is currently using the Internet. It continues to be quite an elitist medium. Thus, almost by definition at this point, anyone who is now using the Web is probably a fairly smart person. Given this, it makes no sense to blame users' difficulty with a site or design on stupidity. When current Web users have problems, it's because the design is too difficult.

    Looking Ahead
    As the Internet keeps growing, it will reach ever-broader segments of the population. Five years from now we might in fact have people online who could be indelicately described as stupid. Whether or not such people should be included or excluded from the Web is a political and social question:
    What percentage of the population can we exclude from the new economy?

    From my perspective, the answer is "very few." Politicians might say "zero," which is an honorable but unrealistic goal. Literacy offers a good analogy here: While all rich countries aim at zero illiteracy, there are always some children who don't learn to read. Nonetheless, we cannot accept high illiteracy rates and expect to maintain a prosperous society.

    As far as meeting the need for Internet usability, we have yet to scratch the surface. Very few websites are easy enough to continue supporting users when the Internet reaches 80% of the population. To serve 95% of the population (let alone 99%), substantial advances in usability will be required.

    Disregarding political and moral issues, the broadening user base poses a very simple business question: What percentage of your prospects will you turn away because they are not smart enough to use your website? Maybe 10% of your potential customers? Or perhaps 20%? That's a lot of dollars lost to an elitist attitude.

    And, even if you accept a 20% loss in customers because your site is too difficult, you still need a site easy enough for 80% of the population to use. Considering that most sites are too difficult for the 50% of the population that is currently online, companies will have to substantially improve their usability to willingly abandon that "acceptable" 20%.

    George Burnad Shah
    GeneralRe: Request to Author Pin
    Christian Graus22-Jan-03 17:03
    protectorChristian Graus22-Jan-03 17:03 
    GeneralRe: Request to Author Pin
    Anonymous22-Jan-03 17:11
    Anonymous22-Jan-03 17:11 
    GeneralRe: Request to Author Pin
    25-Jan-03 1:41
    suss25-Jan-03 1:41 
    GeneralRe: Request to Author Pin
    Anonymous5-Feb-03 17:37
    Anonymous5-Feb-03 17:37 
    GeneralRe: Request to Author Pin
    speedpacer15-Jun-03 7:57
    speedpacer15-Jun-03 7:57 
    GeneralRe: Request to Author Pin
    rubbish@eezi.nl18-Mar-05 8:01
    rubbish@eezi.nl18-Mar-05 8:01 
    GeneralRe: Request to Author Pin
    iodus5-Oct-05 23:55
    iodus5-Oct-05 23:55 
    GeneralA very good article and code Pin
    Muhammad Ahmed27-Sep-02 2:33
    Muhammad Ahmed27-Sep-02 2:33 
    GeneralRe: A very good article and code Pin
    Aisha Ikram17-Nov-03 17:52
    Aisha Ikram17-Nov-03 17:52 
    GeneralWon't work on SYSTEM.INI Pin
    Anonymous25-Sep-02 9:15
    Anonymous25-Sep-02 9:15 
    GeneralRe: Won't work on SYSTEM.INI Pin
    Aisha Ikram25-Sep-02 17:41
    Aisha Ikram25-Sep-02 17:41 
    GeneralRe: Won't work on SYSTEM.INI Pin
    Anonymous26-Sep-02 10:55
    Anonymous26-Sep-02 10:55 
    GeneralRe: Won't work on SYSTEM.INI Pin
    Anonymous2-Nov-02 12:48
    Anonymous2-Nov-02 12:48 
    GeneralRe: Won't work on SYSTEM.INI Pin
    Anonymous7-Nov-02 23:19
    Anonymous7-Nov-02 23:19 
    GeneralRe: Won't work on SYSTEM.INI Pin
    Philippe Lhoste4-Dec-02 4:07
    Philippe Lhoste4-Dec-02 4:07 
    GeneralGreat Job Pin
    Tili25-Sep-02 8:07
    Tili25-Sep-02 8:07 
    GeneralRe: Great Job Pin
    Aisha Ikram25-Sep-02 17:37
    Aisha Ikram25-Sep-02 17:37 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.