Click here to Skip to main content
15,884,425 members
Articles / Desktop Programming / MFC

A Simple Class to Read and Write INI Files

Rate me:
Please Sign up or sign in to vote.
3.08/5 (16 votes)
30 Nov 2006CPOL1 min read 108K   4.8K   43  
This class is a thin MFC wrapper around Win32 API for reading/writing INI files.
#if !defined(AFX_INIFILE_H__59955693_F4CE_4E47_9D52_EEA1B6E8F706__INCLUDED_)
#define AFX_INIFILE_H__59955693_F4CE_4E47_9D52_EEA1B6E8F706__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// IniFile.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CIniFile command target

class CIniFile : public CObject
{
public:
	CIniFile(LPCTSTR lpIniFileName, INT iMaxStringLength);
	virtual ~CIniFile();

// Attributes
protected:
	CString		m_strFileName;	// path to the INI file
	const INT	m_MAXSTRLEN;	// max length of a string (excluding the key name) that can be wtritten/read to/from the INI file by this instance

// Implementation	
public:
	CString	GetIniFileName();
	void	SetIniFileName(LPCTSTR lpIniFileName);
	BOOL	GetStatus(CFileStatus& rStatus);

	BOOL	GetString(LPCTSTR lpSection, LPCTSTR lpKey, CString& strRet, LPCTSTR strDefault);
	UINT	GetInt(LPCTSTR lpSection, LPCTSTR lpKey, INT iDefaultValue);	
	FLOAT	GetFloat(LPCTSTR lpSection, LPCTSTR lpKey, FLOAT fDefaultValue);
	BOOL	GetStruct(LPCTSTR lpSection, LPCTSTR lpKey, LPVOID lpRetStruct, UINT iSizeStruct);
	void	GetSectionNames(CStringList& lstSectionNames);

	
	BOOL	WriteSection(LPCTSTR lpSection, LPCTSTR lpData); 
	BOOL	WriteString(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpString);
	BOOL	WriteNumber(LPCTSTR lpSection, LPCTSTR lpKey, INT iValue);
	BOOL	WriteNumber(LPCTSTR lpSection, LPCTSTR lpKey, FLOAT fValue);
	BOOL	WriteStruct(LPCTSTR lpSection, LPCTSTR lpKey, LPVOID lpStruct, UINT iSizeStruct);

	BOOL	RemoveKey(LPCTSTR lpSection, LPCTSTR lpKey);

protected:
	BOOL	GetString(LPCTSTR lpSection, LPCTSTR lpKey, CString& strRet, LPCTSTR strDefault, DWORD iSize);
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_INIFILE_H__59955693_F4CE_4E47_9D52_EEA1B6E8F706__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer Prolitech
United States United States
doing business as Prolitech
Redwood City, CA

blog (mostly technical)
http://prolifictec.com/blog/index.html

Comments and Discussions