For the first time I wrote the article, I don't know how to upload an attachment, I can only put the code on the code.
A few days ago, I used to read INI files, I did an ini class, I hope I can give you a reference.
I don't explain the role of each function, you can see it from the name.
I also hope that if you find a mistake, please correct Mail, miaoxf@126.com
// InIfile.h: interface for the cinifile class.////
#if! defined (AFX_INIFILE_H__3EF6D649_6870_480B_BA94_D135F75D8C2A__INCLUDED _) # Define AFX_INIFILE_H__3EF6D649_6870_480B_BA94_D135F75D8C2A__INCLUDED_
#if _MSC_VER> 1000 # prgma overce # endif //_MSC_VER> 1000
Class cinifile {public: cstring getClassVersion (); DWORD INIFILE_GETKEYNAMES (CSTRING STRSECTIONNAME, CSTRINGARRAY & Strarray);
Uint getMaxSize_a_section (); uint getMaxSize_all_sectionNames ();
DWORD IniFile_GetKeyNamesValues (CString strSectionName, CStringArray & strArray); BOOL IniFile_GetStruct (LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPVOID lpStruct, UINT uSizeStruct); DWORD IniFile_GetSectionNames (CStringArray & strArray);
BOOL IniFile_DeleteStruct (LPCTSTR lpSectionName, LPCTSTR lpKeyName); BOOL IniFile_DeleteSection (CString strSectionName); BOOL IniFile_DeleteString (CString strSectionName, CString strKeyName);
BOOL INIFILE_MODIFYSTRING (LPCTSTSTR LPSECTIONNAME, LPCTSTSTR LPKEYNAME, LPCTSTSTR LPSTRING); BOOL INIFILE_MODIFYINT (LPCTSTSTR LPSECTIONNAME, LPCTSTSTR LPKEYNAME, INT NVALUE);
BOOL IniFile_WriteStruct (LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPVOID lpStruct, UINT uSizeStruct); BOOL IniFile_WriteSection (LPCTSTR lpSectionName, LPCTSTR lpString); BOOL IniFile_WriteLongString (LPCTSTR lpValue, LPCTSTR lpSectionName, LPCTSTR lpKeyName, ...);
Bool INIFILE_WRITESTRING (LPCTSTSTR LPSECTIONNAME, LPCTSTSTR LPKEYNAME, LPCTSTSTR LPSTRING); BOOL INIFILE_WRITEINT (LPCTSTSTR LPSECTIONNAME, LPCTSTSTLPKEYNAME, INT NVALUE);
DWORD IniFile_GetString (CString strSectionName, CString strKeyName, CString strDefault, CString & strReturnedString); DWORD IniFile_GetString (LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize); UINT IniFile_GetInt (LPCTSTR lpSectionName, LPCTSTR lpKeyName, INT nDefault); BOOL EnsureIniFileExist ();
CString getfilepathname (); void setdefaultfilepathname (); bool setfilename;
Void setMaxSize_all_sectionnames; void setMaxSize_a_section (uint nsize);
Cinifile (); virtual ~ cinifile (); protected: bool isinifileexist (); void inifile_initializeforcreate ();
DWORD INIFILE_GETSECTIONNAMES (LPTSTSTSZRETURNBuffer, DWORD NSIZE); DWORD INIFILE_GETSECTION (LPTSTR LPSECTIONNAME, LPTSTSTR LPRETURNEDSTRING, DWORD NSIZE);
PRIVATE: CSTRING M_STRFILEFULLNAME; UINT NMAXSIZE_ALL_SECTIONNAMES; uint nmaxsize_a_section;};
#ndif //! defined (AFX_INIFILE_H__3EF6D649_6870_480B_BA94_D135F75D8C2A__included_)
// CPP file
// InIfile.cpp: Implementation of the cinifile class.////
#include "stdafx.h"
#include "inIfile.h"
#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif
//// construction / destruction //
CINIFILE :: CINIFILE () {nmaxsize_all_sectionNames = 1024; nmaxsize_a_section = 1024; setdefaultfilepathname ();
CINIFILE :: ~ cinifile () {
}
Bool cinifile :: isinifileexist () {cfile myfile; if (! Myfile.open (m_strfilefullname, cfile :: moderead) Return 0; myfile.close (); return 1;}
Bool Cinifile :: EnSureiniFighExist () {ix (isinifileexist ()) Return 1;
Cfile myfile;
IF (! myfile.open (m_strfilefullname, cfile :: modecreate) Return False;
Myfile.close ();
InIfile_initializeForcreate (); return} void cinifile :: setDefaultfilepathname () {tchar exefullpath [max_path]; int Len2 = getModuleFileName (null, exefullpath, max_path);
CString strdir (EXEFULLPATH); strDir.trimleft (); strdir.trimright ();
CString straTemp = strdir.Left (strdir.getLength () - 3); strdir = strTemp; strdir = _t ("ini"); m_strfilefullname = strdir; return;
BOOL CIniFile :: IniFile_WriteString (LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTSTR lpString) {return WritePrivateProfileString (lpSectionName, lpKeyName, lpString, m_strFileFullName);}
BOOL CIniFile :: IniFile_WriteInt (LPCTSTR lpSectionName, LPCTSTR lpKeyName, int nValue) {CString strTemp; strTemp.Format (_T ( "% d"), nValue); return WritePrivateProfileString (lpSectionName, lpKeyName, strTemp, m_strFileFullName);}
UINT CINIFILE :: Inifile_Getint (LPCTSTR LPSECTIONNAME, LPCTSTR LPKEYNAME, INT NDEFAULT) {Return GetPrivateProfileint (lpsectionname, lpkeyname, ndefault, m_strfilefullname);}
DWORD CIniFile :: IniFile_GetString (LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize) {return GetPrivateProfileString (lpSectionName, lpKeyName, lpDefault, lpReturnedString, nSize, m_strFileFullName);}
DWORD CINIFILE :: Inifile_getstring (CString StrKeyName, CString strdefault, cstring & strreturnedstring) {
CHAR BUF [256]; DWORD LEN = GetPrivateProfileString (StRSECTIONNAME, StrKeyname, StrDefault, BUF, 256, M_StrfileFullname); BUF [LEN] = 0;
StrreturnedString.Format ("% s", buf); return gen;}
CString CIniFile :: GetFilePathName () {return m_strFileFullName;} BOOL CIniFile :: IniFile_ModifyInt (LPCTSTR lpSectionName, LPCTSTR lpKeyName, int nValue) {return IniFile_WriteInt (lpSectionName, lpKeyName, nValue);}
Bool cinifile :: Inifile_ModifyString (LPCTSTR LPSECTIONNAME, LPCTSTSTR LPKEYNAME, LPCTSTSTR LPSTRING) {Return Inifile_WritString (lpsectionname, lpKeyname, lpstring);}
Bool cinifile :: InIfile_WriteLongString (LPCTSTR LPVALUE, LPCTSTSTSTSTSTSTSTSTSECTIONNAME, LPCTSTSTR LPKEYNAME, ...) {// This function does not allow the key name and key value to be empty
VA_LIST PARGLIST; LPCTSTR P;
IF (lpvalue == null || lpsectionname == null || lpKeyName == null) Return False;
CString StrsectionName (lpsectionname); cstring strkeyName (LPKEYNAME);
Bool result = false;
Va_start (Parglist, LpKeyname); / * Initialize Variable Arguments. * /
Do {P = Va_arg (Parglist, LPCTSTR); CSTRING STRTEMPVALUE1 (P);
IF (strTempValue1 == "Listend" || strTempValue1 ==_T (")) Break;
If (! inIfile_writestring (strsectionname, strkeyname, strtempvalue1)) Return False;
P = VA_ARG (PARGLIST, LPCTSTS; CSTRING STRTEMPVALUE2 (P);
IF (strTempValue2 == "Listend" || STRTEMPVALUE2 ==_T (")) Return False;
STRSECTIONNAME = strTempValue1; strkeyName = startempvalue2;} while (1);
VA_END (PARGLIST); / * RESET VARIABLE ARGUMENTS. * /
IF (! inIfile_writestring (strsectionname, strkeyname, lpvalue) Return False; Return True;
}
Bool Cinifile :: InIfile_DeleteString (CString strkeyname) {Return WritePrivateProfileString (StRSECTIONNAME, STRKEYNAME, NULL, M_STRFILEFULLNAME);}
BOOL CIniFile :: IniFile_DeleteSection (CString strSectionName) {return WritePrivateProfileString (strSectionName, NULL, NULL, m_strFileFullName); // return WritePrivateProfileSection (strSectionName, NULL, m_strFileFullName); // This method will make the title of section be deleted
// Return WritePrivateProfileSection (strsectionname, "", m_strfilefullname); // This method will retain the title of section}
Bool cinifile :: InIfile_Writection (lpctstr lpstence "{return writeprivateprofilesection (lpsectionname, lpstring, m_strfilefullname);}
BOOL CIniFile :: IniFile_WriteStruct (LPCTSTR lpSectionName, LPCTSTR lpszKey, LPVOID lpStruct, UINT uSizeStruct) {return WritePrivateProfileStruct (lpSectionName, lpszKey, lpStruct, uSizeStruct, m_strFileFullName);}
Bool Cinifile :: InIfile_deleteStruct (LPCTSTR LPSECTIONNAME, LPCTSTR LPSZKEY) {Return WritePrivateProfileStruct (LPSECTIONNAME, LPSZKEY, NULL, 1, M_STRFILEFULLNAME);}
Bool Cinifile :: setFileName (LPCTSTR LPFILENAME) {if (! Lpfilename) Return False;
CString StrfileName; cstring strpathname; strfilename.trimleft (); strfilename.trimright ();
IF (strfilename.find ('//') == -1) {tchar exefullpath [max_path];
INT LEN2 = getModuleFileName (null, exefullpath, max_path); cstring strdir (exefullpath);
Strdir.trimleft (); strdir.trimright (); int index = strdir.reverseFind ('//'); strpathname = strdir.Left (index 1) strfilename;} else strpathname = strfilename;
CString Str;
IF (strpathname.getlength ()> 4) {str = strpathname.right (4); str.maker ();
IF (str == _t ("ini")) {m_strfilefullname = strpathname; return true;}}
StrPathname = _t (". ini"); m_strfilefullname = strpathname; return true;}
Void cinifile :: Inifile_Initializeforcreate () {InIfile_WritString ("Administrator", "SA", "SA / R / N");
INIFILE_WRITESTRING ("User", "ABC", "ABC"); inIfile_WritString ("User", "AB", "AB / R / N");}
DWORD CINIFILE :: INIFILE_GETSECTIONNAMES (LPTSTSTSZRETURNBuffer, DWORD NSIZE) {Return GetPrivateProfileeSectionNames (lpszreturnBuffer, nsize, m_strfilefullname);}
DWORD CINIFILE :: INIFILE_GETSECRAY {Char * SZ = new char [nmaxsize_all_sectionNames]; DWORD DW = INIFILE_GETSECTIONNAMES (SZ, NMAXSIZE_ALL_SECTIONNAMES);
Strarray.removeAll ();
Char * index = sz; do {cstring str (index); if (str.getlength () <1) {delete [] sz; return dw;} stratay.add (STR);
Index = index str.getLength () 1;} while (INDEX && (INDEX && (Index Return dw;} BOOL CIniFile :: IniFile_GetStruct (LPCTSTR lpSectionName, LPCTSTR lpszKey, LPVOID lpStruct, UINT uSizeStruct) {return GetPrivateProfileStruct (lpSectionName, lpszKey, lpStruct, uSizeStruct, m_strFileFullName); } DWORD CIniFile :: IniFile_GetSection (LPCTSTR lpSectionName, LPTSTR lpReturnedString, DWORD nSize) {return GetPrivateProfileSection (lpSectionName, lpReturnedString, nSize, m_strFileFullName); } DWORD CINIFILE :: Inifile_getKeynamesvalues (CString StrsectionName, CStringArray & Strarray) { Char * sz = new char [nmaxsize_a_section]; DWORD DW = INIFILE_GETSECTION (STRSECTIONNAME, SZ, NMAXSIZE_A_SECTION); Char * index = SZ; cstring strname, strval; int nposition = -1; While (Index && (INDEX IF (str.getlength () <1) {delete [] sz; return dw;} if ((nposition = str.find (_t ('='))) == -1) {inIfile_deleteString (STRSECTIONNAME, STR); // contininer;} else {strname = str.Left (nposition); strValue = str.mid (nPosition 1); / * if (Str screen.getLength () <1) {InIfile_DeleteString (STRSECTIONNAME, STRNAME); // Continue } * / Strarray.Add (Strname); straray.add (strval);} Index = index str.getLength () 1;} Delete [] SZ; Return DW; Void cinifile :: setMaxSize_a_section (uint nsize) {nmaxSize_a_section = nsize;} Void cinifile :: setMaxSize_all_sectionNames (uint nsize) {nmaxsize_all_sectionnames = nsize;} UINT CINIFILE :: getMaxSize_all_sectionNames () {return nmaxsize_all_sectionNames; UINT CINIFILE :: getMaxSize_a_section () {return nmaxsize_a_section; DWORD CINIFILE :: Inifile_getKeynames (CString StrsectionName, CStringArray & Strarray) { Char * sz = new char [nmaxsize_a_section]; DWORD DW = INIFILE_GETSECTION (STRSECTIONNAME, SZ, NMAXSIZE_A_SECTION); Char * index = sz; CString Strname, Strvalue; int nposition = -1; While (Index && (INDEX IF (str.getlength () <1) {delete [] sz; return dw;} if ((nposition = str.find (_t ('='))) == -1) {inIfile_deleteString (STRSECTIONNAME, STR); // contininer;} else {strname = str.Left (nposition); Strarray.Add (Strname); Index = index str.getLength () 1;} Delete [] SZ; Return DW;