资源简介
linux下读写ini文件方法,0分可直接下载

代码片段和文件信息
#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
相关资源
- uboot到linux logo显示不间断 补丁
- UNIX/LINUX编程实践教程的源码
- Linux任务管理器
- linux应用层的华容道游戏源代码
- ubuntu9.10 可加载内核模块和字符设备驱
- MP3文件ID3v2ID3v2APEv2标签读取
- 操作系统实验——虚存管理实验
- linux下的发包工具sendip
- 尚观培训linux许巍关于c 的笔记和讲义
- 尚观培训linux董亮老师关于数据结构的
- linux 线程池源码 c 版
- linux C 电梯程序练习
- linux下用多进程同步方法解决生产者
- Linux 操作系统实验(全)
- Linux From Scratch 中文手册
- linux 网络实验 ftp程序
- Linux命令大全离线版&在线版
- 操作系统共享内存实验
- dos 下运行Linux 命令--gnu_utils
- linux 0.12内核源代码
- linux简易shell C实现
- linux实验报告及心得体会
- 基于GTK的Linux环境下的简易任务管理器
- linux扫雷游戏代码
- CAN Linux驱动代码
- Linux系统教材
- intel 82579LM 网卡驱动Linux系统版 v1.9.
- SA1110处理器掌上电脑液晶显示器设计
- 基于Linux的串口服务器设计
- Windows下访问LINUX的利器-SSH
评论
共有 条评论