C + + Read INI config file class instance detailed _c language

Source: Internet
Author: User
Tags class definition ini

This article illustrates the method of reading a configuration file in C + + with an example.

In general, we like to use the INI extension file as a configuration file, you can read and modify variable values, you can set up a new group, a new variable, the example code of this article is to read the INI definition file, the other is the Cinifile class implementation file, the combination of the two, the perfect realization Read and write to the INI file.

User Interface Description: In the member function SETVARSTR and Setvarint functions, when itype equals zero, if the user-drawn parameter does not exist in the INI file, the new variable is written. When the itype is not equal to zero, if the user-drawn parameters do not exist in the INI file, Instead of writing a new variable, it returns false directly. The program code is as follows, some of the key points are annotated:

/*================================================================== = filename: cinifile class definition file = Main function: can read. Modify variable values, You can set a new group, a new variable ====================================================================*/#ifndef _cinifile_h_ #define _ Cinifile_h_ #include <afxtempl.h>//user Interface Description: In member functions SETVARSTR and Setvarint functions, when itype equals zero, if the user-drawn parameters do not exist in the INI file,//
The new variable is written. When the itype is not equal to zero, if the user-made parameters do not exist in the INI file, they are not written to the new variable, but are returned directly to false;
 Class Cinifile {public:cinifile ();
Virtual ~cinifile ();
 Private:cinifile (const cinifile &);
Cinifile & operator = (const cinifile &);
 Public://Create function BOOL Create (const CString &strfilename);
 Get the variable integer value BOOL getvarint (const CString &,const CString &, int &);
 Get the variable string value BOOL getvarstr (const CString &,const CString &, CString &);
 Reset variable integer value BOOL setvarint (const CString &,const CString &, const int &,const int itype = 1); Reset variable String value BOOL setvarstr (const CString &,const CString, const CString &,const int Itype = 1);
 Private:bool GetVar (const CString &,const CString &,cstring &);
 BOOL SetVar (const CString &,const CString &,const CString &,const int itype = 1);
int Searchline (const CString &,const CString &);
 Private:carray Filecontainer;
 BOOL Bfileexsit;
 CStdioFile Stffile;
CString Strinifilename;
}; #endif CIniFile.cpp file contents: #include <afxtempl.h> #include "CIniFile.h" Cinifile::cinifile (): Bfileexsit (FALSE) {} C Inifile::~cinifile () {if (bfileexsit) {if (Stffile.open strinifilename,cfile::modecreate | Cfile::modewrite) && filecontainer.getsize () > 0) {CString strparam; for (int i = 0; i< filecontainer.getsiz
E (); i++) {strparam = Filecontainer[i];//stffile.writestring (Strparam); Stffile.writestring (strparam+_t ("\ n"));}
Stffile.close ();
} if (Filecontainer.getsize () > 0) {filecontainer.removeall ();}} BOOL cinifile::create (const CString & strFileName) {bfileexsit = FALSE; strinifilename = strFileName; if (!stffile.ope NStrfilename,cfile::moderead)) {return bfileexsit;}
CString Strfileline;
while (Bfileexsit = stffile.readstring (strfileline)) {if (bfileexsit = = FALSE) return bfileexsit;
Filecontainer.add (Strfileline);
} stffile.close ();
Bfileexsit = TRUE;
return bfileexsit; BOOL Cinifile::getvar (const CString & strsection,const CString & strvarname,cstring &strreturnvalue) {if (b Fileexsit = = FALSE | |
Filecontainer.getsize () < 0) return bfileexsit;
int iline = Searchline (strsection,strvarname);
if (Iline > 0) {CString strparam = filecontainer[iline-1]; strreturnvalue = Strparam.mid (Strparam.find (_t ("=")) + 1);
return TRUE;
return FALSE; BOOL cinifile::getvarstr (const CString & strsection,const CString & strvarname,cstring &strreturnvalue) {R
Eturn (GetVar (Strsection,strvarname,strreturnvalue)); BOOL cinifile::getvarint (const CString & strsection,const CString & Strvarname,int & ivalue) {CString Strre
Turnvar; if (GetVar (Strsection,strvarname,strreturnvaR)) {strreturnvar.trimleft (); int ilen = Strreturnvar.getlength (); ivalue = _tstoi (Strreturnvar.getbuffer (ILen));
return TRUE;
return TRUE;  BOOL Cinifile::setvar (const CString & strsection,const CString & strvarname,const CString & Strvar,const int Itype) {if (bfileexsit = = FALSE) return bfileexsit; if (filecontainer.getsize () = = 0) {Filecontainer.add (_t ("[") + Strse
ction + _t ("]");
Filecontainer.add (Strvarname + _t ("=") + Strvar);
return TRUE;
int i = 0;
int ifilelines = (int) filecontainer.getsize (); For (Pinterator;pinterator!= filecontainer.end (), ++pinterator)//{while (i< ifilelines) {CString strvalue =
Filecontainer.getat (i++);
Strvalue.trimleft (); if ((Strvalue.find (_t ([)) >=0) && (Strvalue.find (strsection) >=0)) {while (I < ifilelines) {CString St
Rsectionlist = filecontainer[i++];
Strsectionlist.trimleft ();
if (Strsectionlist.find (_t ("//)) >=0)//Find comment line continue; if (Strsectionlist.find (strvarname) >=0)//Find {CString strparam = StRvarname + "=" + Strvar;
Filecontainer.setat (I-1,strparam);
Filecontainer[i-1] = strparam;
return TRUE; if (Strsectionlist.find _t ("["), 0) >=0)//In the section of the original file, there is no corresponding variable to add and, in this case, there are other section {//process flows below).
First, move the current value backward in turn, and then add the new value if (Itype!=0) to the current position to return FALSE;
CString Strparam;
Filecontainer.add (Strparam);
int ipre = (int) (Filecontainer.getsize ()-1);
while (Ipre >= i) {CString strbehind = filecontainer[ipre-1];
Filecontainer[ipre] = Strbehind;
Ipre--;
} strparam = Strvarname + _t ("=") + Strvar;
Filecontainer.setat (I-1,strparam);
return TRUE;
} if (i = = ifilelines && itype = 0) {filecontainer.add (strvarname + _t ("=") + Strvar); return TRUE;}
} if (Itype = 0) {Filecontainer.add (_t ("[") + Strsection + _t ("]"));
Filecontainer.add (Strvarname + _t ("=") + Strvar);
return TRUE; BOOL cinifile::setvarstr (const CString & strsection,const CString & strvarname,const CString & Strvalue,con St int Itype) {return SetVar (strsection,strvarname,strvalue,itype); BOOL cinifile::setvarint (const CString & strsection,const CString & strvarname,const int & Ivalue,const int Itype) {CString strvar; Strvar.format (_t ("%d"), ivalue); return (SetVar (strsection,strvarname,strvar,itype)); int
Cinifile::searchline (const CString & strsection,const CString & Strvarname) {if (filecontainer.getsize () > 0) {int i = 0; int ifilelines = (int) filecontainer.getsize (); while (i< ifilelines) {CString strvalue = filecontainer[i++
];
Strvalue.trimleft (); if (Strvalue.find (_t ("[")) >=0 && strvalue.find (strsection,1) >=0) {while (I < ifilelines) {CString STRs
Ectionlist = filecontainer[i++];
Strsectionlist.trimleft ();
if (Strsectionlist.find (_t ("//)) >=0)//Find comment line continue;
if (Strsectionlist.find (strvarname) >=0)//Find {return i;} if (Strsectionlist.find (_t ("["), 0) >= 0)//Another paragraph appears, looking for failure
{return-2;}
}}} return-1; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.