C++读写ini文件记录总结

// IniFile.cpp: implementation of the CIniReader class.
//
//

//#include "stdafx.h"
//#include "Readini.h"
#include "INI.h"
#include 

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

//
// Construction/Destruction
//



// Used to retrieve a value give the section and key
CString CIniReader::getKeyValue(CString strKey,CString strSection)
{
	char ac_Result[255];

	// Get the info from the .ini file	
	m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey,
		"",ac_Result, 255, (LPCTSTR)m_strFileName);	

	CString strResult(ac_Result);
	return strResult;
}

// Used to add or set a key value pair to a section
long CIniReader::setKey(CString strValue, CString strKey, CString strSection)
{
	m_lRetValue = WritePrivateProfileString (strSection, strKey, 
                             strValue, m_strFileName);

	return m_lRetValue;
}

// Used to find out if a given section exists
BOOL CIniReader::sectionExists(CString strSection)
{
	char ac_Result[100];	
	CString csAux;
	// Get the info from the .ini file	
	m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,NULL,
		"",ac_Result, 90, (LPCTSTR)m_strFileName);
	// Return if we could retrieve any info from that section
	return (m_lRetValue > 0);
}

// Used to retrieve all of the  section names in the ini file
CStringList* CIniReader::getSectionNames()  //returns collection of section names
{
	char ac_Result[2000];
	m_sectionList->RemoveAll();
	
	m_lRetValue = GetPrivateProfileSectionNames(ac_Result,2000,(LPCTSTR)m_strFileName);
	
	CString strSectionName;
	for(int i=0; iInsertAfter(m_sectionList->GetTailPosition(),strSectionName);
			}
			strSectionName = "";
		}
	}

	return m_sectionList;
}

// Used to retrieve all key/value pairs of a given section.  
CStringList* CIniReader::getSectionData(CString strSection)  
{
	char ac_Result[2000];  //change size depending on needs
	m_sectionDataList->RemoveAll();
	m_lRetValue = GetPrivateProfileSection((LPCTSTR)strSection, ac_Result, 2000, (LPCTSTR)m_strFileName);

	CString strSectionData;
	for(int i=0; iInsertAfter(m_sectionDataList->GetTailPosition(),strSectionData);
			}
			strSectionData = "";
		}
	}

	return m_sectionDataList;
}

void CIniReader::setINIFileName(CString strINIFile)
{
	m_strFileName = strINIFile;
}

BOOL CIniReader::fileExists()
{
	LPCTSTR lpFilePath = m_strFileName;
	WIN32_FIND_DATA   wfd;   
	BOOL   bRet;   
	HANDLE   hFind;   
	hFind = FindFirstFile(lpFilePath, &wfd);   
	bRet  = hFind != INVALID_HANDLE_VALUE;   
	FindClose(hFind);   
	return   bRet;   
}


// IniFile.h: interface for the CIniReader class.
//
//

#if !defined(AFX_INIFILE_H__99976B4B_DBA1_4D1E_AA14_CBEB63042FD1__INCLUDED_)
#define AFX_INIFILE_H__99976B4B_DBA1_4D1E_AA14_CBEB63042FD1__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include 

class CIniReader  
{
public:
	BOOL fileExists(void);//检查文件是否存在
	// 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;
};

#endif // !defined(AFX_INIFILE_H__99976B4B_DBA1_4D1E_AA14_CBEB63042FD1__INCLUDED_)


#include "INI.h"
#include 
#include 
using namespace  std;

int main(void)
{
	CIniReader iniReader;
	iniReader.setINIFileName(".\\parameter.ini");
	if(iniReader.fileExists() != TRUE)
	{
		printf("the file parameter doesn't exist!");
		return 0;
	}
	
	if ((iniReader.sectionExists("Parameter")) == TRUE )
	{
		UINT recTime;
		UINT serachGapTime;
		CString strRecTime;
		CString strSerGapTime;
		strRecTime = iniReader.getKeyValue("RecTime", "Parameter");
		strSerGapTime = iniReader.getKeyValue("SerachGapTime", "Parameter");
		recTime = atoi(strRecTime);
		serachGapTime = atoi(strSerGapTime);
		cout<

ini文件的内容是这个

[Parameter]


;表示同一个任务下次生成与本次处理完后的间隔时间,单位为天
RecTime = 3


;表示检索hitrade数据库的间隔时间,以小时为单位
SerachGapTime = 1



你可能感兴趣的:(技术,C++,/MFC)