资源简介
GetPrivateProfileString
GetPrivateProfileInt
WritePrivateProfileString
GetPrivateProfileSectionNames
代码片段和文件信息
#include
#include “inifile.h“
#include
#include
#include
#include
#include
CIniFile::CIniFile(void)
{
}
CIniFile::CIniFile(const char *chFileName):m_strFileName(chFileName)
{
Init();
}
CIniFile::CIniFile(const string &strFileName):m_strFileName(strFileName)
{
Init();
}
CIniFile::~CIniFile(void)
{
}
int CIniFile::Init(const char* pFileName)
{
m_strFileName = pFileName;
return LoadFile();
}
int CIniFile::Init(const string& strFileName)
{
m_strFileName = strFileName;
return LoadFile();
}
int CIniFile::Init()
{
return LoadFile();
}
int CIniFile::Dump()
{
map::iterator tSecIter1 = m_mSec2Config.begin() tSecIter2 = m_mSec2Config.end();
ConfigType::iterator tConfigTypeIter1 tConfigTypeIter2;
while (tSecIter1 != tSecIter2)
{
cout << “[“ << tSecIter1->first << “]“ << endl;
tConfigTypeIter1 = tSecIter1->second.begin() tConfigTypeIter2 = tSecIter1->second.end();
while (tConfigTypeIter1 != tConfigTypeIter2)
{
cout << tConfigTypeIter1->first << “=“ << tConfigTypeIter1->second << endl;
++tConfigTypeIter1;
}
cout << endl;
++tSecIter1;
}
return 0;
}
int CIniFile::ReadItem(const string& strSection const string& strKey const string& strDefault string& strValue)
{
if (!m_mSec2Config.count(strSection))
{
return -1;
}
ConfigType& tConfigType = m_mSec2Config[strSection];
if(tConfigType.count(strKey))
{
strValue = tConfigType[strKey];
return 0;
}
else
{
strValue=strDefault;
return -1;
}
}
int CIniFile::WriteItem(const string& strSection const string& strKey const string& strValue)
{
ConfigType& tConfigType = m_mSec2Config[strSection];
if (tConfigType.count(strKey))
{
;//return -1;
}
tConfigType[strKey] = strValue;
return WriteFile();
}
int CIniFile::LoadFile()
{
FILE* pFile;
if (NULL == (pFile = ::fopen(m_strFileName.c_str() “r“)))
{
return -1;
}
string strLine strSection;
string strKey strValue;
size_t nPos nEndPos;
ConfigType tConfigType;
while (0 == ReadLine(pFile strLine))
{
if (string::npos != (nPos = strLine.find_first_of(“[“)))
{
if (string::npos == (nEndPos = strLine.find_first_of(“]“)))
{
::fclose(pFile);
return -1;
}
strSection = strLine.substr(nPos + 1 nEndPos - nPos - 1);
if (0 > TrimString(strSection))
{
::fclose(pFile);
return -1;
}
}
else if (string::npos != (nPos = strLine.find_first_of(“=“)))
{
strKey = strLine.substr(0 nPos);
strValue = strLine.substr(nPos + 1);
if (0 > TrimString(strKey) || 0 > TrimString(strValue) || strSection.empty())
{
::fclose(pFile);
return -1;
}
m_mSec2Config[strSection][strKey] = strValue;
}
}
return ::fclose(pFile);
}
int CIniFile::WriteFile()
{
FILE* pFile;
if (NULL == (pFile = ::fopen(
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1937 2012-12-11 15:53 inifile.h
文件 14495 2012-12-11 15:53 inifile.cpp
----------- --------- ---------- ----- ----
16432 2
评论
共有 条评论