⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inifileex.cpp

📁 一个简单的读取Ini文件的类
💻 CPP
字号:
// IniFileEx.cpp: implementation of the CIniFileEx class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "IniFileEx.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CIniFileEx::CIniFileEx()
{
	fCreated=FALSE;
}

CIniFileEx::~CIniFileEx()
{

}

void CIniFileEx::Create(CString sFileName)
{
	m_sFileName=sFileName;
	fCreated=TRUE;
}

BOOL CIniFileEx::WriteValue(CString Section, CString Ident, CString Value)
{
	if(!fCreated)return FALSE;
	return WritePrivateProfileString(Section, Ident, Value, m_sFileName);
}

BOOL CIniFileEx::WriteValue(CString Section, CString Ident, int Value)
{
	if(!fCreated)return FALSE;
	CString strValue;
	strValue.Format("%d", Value);
	return WritePrivateProfileString(Section, Ident, strValue, m_sFileName);
}

BOOL CIniFileEx::WriteValue(CString Section, CString Ident, long Value)
{
	if(!fCreated)return FALSE;
	CString strValue;
	strValue.Format("%d", Value);
	return WritePrivateProfileString(Section, Ident, strValue, m_sFileName);
}

BOOL CIniFileEx::ReadValue(CString Section, CString Ident, long &Value, long defValue)
{
	char *pstrReturn=new char[255];
	CString strDef="";
	strDef.Format("%d", defValue);
	if(!GetPrivateProfileString(Section, Ident, strDef, pstrReturn, 30, m_sFileName)){
		Value=0;
		delete []pstrReturn;
		return FALSE;
	}
	Value = atol(pstrReturn);

	delete []pstrReturn;
	return TRUE;
}

BOOL CIniFileEx::ReadValue(CString Section, CString Ident, CString &Value, CString defValue)
{
	char *pstrReturn=new char[30];
	if(!GetPrivateProfileString(Section, Ident, defValue, pstrReturn, 30, m_sFileName)){
		delete []pstrReturn;
		return FALSE;
	}
	Value = pstrReturn;

	delete []pstrReturn;
	return TRUE;
}

BOOL CIniFileEx::ReadValue(CString Section, CString Ident, int &Value, int defValue)
{
	char *pstrReturn=new char[30];
	CString strDef="";
	strDef.Format("%d", defValue);
	if(!GetPrivateProfileString(Section, Ident, strDef, pstrReturn, 30, m_sFileName)){
		Value=0;
		delete []pstrReturn;
		return FALSE;
	}
	Value = atoi(pstrReturn);

	delete []pstrReturn;
	return TRUE;
}

BOOL CIniFileEx::WriteValue(CString Section, CString Ident, CTime Value)
{
	if(!fCreated)return FALSE;
	CString strValue;
	time_t lDate=Value.GetYear()*10000 + Value.GetMonth()*100 + Value.GetDay();
	strValue.Format("%d", lDate);
	return WritePrivateProfileString(Section, Ident, strValue, m_sFileName);

}

BOOL CIniFileEx::ReadValue(CString Section, CString Ident, CTime &Value, CTime defValue)
{
	char *pstrReturn=new char[30];
	CString strDef="";
	time_t lDate=defValue.GetYear()*10000 + defValue.GetMonth()*100 + defValue.GetDay();
	strDef.Format("%d", lDate);

	if(!GetPrivateProfileString(Section, Ident, strDef, pstrReturn, 30, m_sFileName)){
		Value=defValue;
		delete []pstrReturn;
		return FALSE;
	}
	lDate = atol(pstrReturn);
	//Value = CTime(lDate);
	int y,m,d;
	y=(lDate/10000)%10000;
	m=(lDate/100)%100;
	d=lDate%100;
	CTime tmpT(y,m,d,0,0,0);
	Value = tmpT;

	delete []pstrReturn;
	return TRUE;
}

BOOL CIniFileEx::WriteValue(CString Section, CString Ident, DWORD Value)
{
	if(!fCreated)return FALSE;
	CString strValue;
	strValue.Format("%d", Value);
	return WritePrivateProfileString(Section, Ident, strValue, m_sFileName);

}

BOOL CIniFileEx::ReadValue(CString Section, CString Ident, DWORD &Value, DWORD defValue)
{
	char *pstrReturn=new char[30];
	CString strDef="";
	strDef.Format("%d", defValue);
	if(!GetPrivateProfileString(Section, Ident, strDef, pstrReturn, 30, m_sFileName)){
		Value=0;
		delete []pstrReturn;
		return FALSE;
	}
	Value = atol(pstrReturn);

	delete []pstrReturn;
	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -