ini文件的读取

ini.h代码

#ifndef INI_H

#define INI_H

#include <map>

#include <string>

using namespace std;

#define CONFIGLEN           256

enum INI_RES

{

INI_SUCCESS,            //成功

INI_ERROR,              //普通错误

INI_OPENFILE_ERROR,     //打开文件失败

INI_NO_ATTR            //无对应的键值

};

//              子键索引    子键值

typedef map<std::string, std::string> KEYMAP;

//              主键索引 主键值

typedef map<std::string, KEYMAP> MAINKEYMAP;

// config 文件的基本操作类

class CIni

{

public:

// 构造函数

CIni();

// 析够函数

virtual ~CIni();

public:

//获取整形的键值

int  GetInt(const char* mAttr, const char* cAttr);

//获取键值的字符串

char *GetStr(const char* mAttr, const char* cAttr);

// 打开config 文件

INI_RES OpenFile(const char* pathName, const char* type);

// 关闭config 文件

INI_RES CloseFile();

protected:

// 读取config文件

INI_RES GetKey(const char* mAttr, const char* cAttr, char* value);

protected:

// 被打开的文件局柄

FILE* m_fp;

char  m_szKey[CONFIGLEN];

MAINKEYMAP m_Map;

};

#endif

ini.cpp代码

#include "ini.h"

/******************************************************************************

* 功  能:构造函数

* 参  数:无

* 返回值:无

* 备  注:

******************************************************************************/

CIni::CIni()

{

memset(m_szKey, 0, sizeof(m_szKey));

m_fp = NULL;

}

/******************************************************************************

* 功  能:析构函数

* 参  数:无

* 返回值:无

* 备  注:

******************************************************************************/

CIni::~CIni()

{

m_Map.clear();

}

/******************************************************************************

* 功  能:打开文件函数

* 参  数:无

* 返回值:

* 备  注:

******************************************************************************/

INI_RES CIni::OpenFile(const char* pathName, const char* type)

{

string szLine, szMainKey, szLastMainKey, szSubKey;

char strLine[CONFIGLEN] = { 0 };

KEYMAP mLastMap;

int  nIndexPos = -1;

int  nLeftPos = -1;

int  nRightPos = -1;

m_fp = fopen(pathName, type);

if (m_fp == NULL)

{

printf("open inifile %s error!\n", pathName);

return INI_OPENFILE_ERROR;

}

m_Map.clear();

while (fgets(strLine, CONFIGLEN, m_fp))

{

szLine.assign(strLine);

//删除字符串中的非必要字符

nLeftPos = szLine.find("\n");

if (string::npos != nLeftPos)

{

szLine.erase(nLeftPos, 1);

}

nLeftPos = szLine.find("\r");

if (string::npos != nLeftPos)

{

szLine.erase(nLeftPos, 1);

}

//判断是否是主键

nLeftPos = szLine.find("[");

nRightPos = szLine.find("]");

if (nLeftPos != string::npos && nRightPos != string::npos)

{

szLine.erase(nLeftPos, 1);

nRightPos--;

szLine.erase(nRightPos, 1);

m_Map[szLastMainKey] = mLastMap;

mLastMap.clear();

szLastMainKey = szLine;

}

else

{

//是否是子键

if (nIndexPos = szLine.find("="), string::npos != nIndexPos)

{

string szSubKey, szSubValue;

szSubKey = szLine.substr(0, nIndexPos);

szSubValue = szLine.substr(nIndexPos + 1, szLine.length() - nIndexPos - 1);

mLastMap[szSubKey] = szSubValue;

}

else

{

//TODO:不符合ini键值模板的内容 如注释等

}

}

}

//插入最后一次主键

m_Map[szLastMainKey] = mLastMap;

return INI_SUCCESS;

}

/******************************************************************************

* 功  能:关闭文件函数

* 参  数:无

* 返回值:

* 备  注:

******************************************************************************/

INI_RES CIni::CloseFile()

{

if (m_fp != NULL)

{

fclose(m_fp);

m_fp = NULL;

}

return INI_SUCCESS;

}

/******************************************************************************

* 功  能:获取[SECTION]下的某一个键值的字符串

* 参  数:

*  char* mAttr  输入参数    主键

*  char* cAttr  输入参数 子键

*  char* value  输出参数 子键键值

* 返回值:

* 备  注:

******************************************************************************/

INI_RES CIni::GetKey(const char* mAttr, const char* cAttr, char* pValue)

{

KEYMAP mKey = m_Map[mAttr];

string sTemp = mKey[cAttr];

strcpy(pValue, sTemp.c_str());

return INI_SUCCESS;

}

/******************************************************************************

* 功  能:获取整形的键值

* 参  数:

*       cAttr                     主键

*      cAttr                     子键

* 返回值:正常则返回对应的数值 未读取成功则返回0(键值本身为0不冲突)

* 备  注:

******************************************************************************/

int CIni::GetInt(const char* mAttr, const char* cAttr)

{

int nRes = 0;

memset(m_szKey, 0, sizeof(m_szKey));

if (INI_SUCCESS == GetKey(mAttr, cAttr, m_szKey))

{

nRes = atoi(m_szKey);

}

return nRes;

}

/******************************************************************************

* 功  能:获取键值的字符串

* 参  数:

*       cAttr                     主键

*      cAttr                     子键

* 返回值:正常则返回读取到的子键字符串 未读取成功则返回"NULL"

* 备  注:

******************************************************************************/

char *CIni::GetStr(const char* mAttr, const char* cAttr)

{

memset(m_szKey, 0, sizeof(m_szKey));

if (INI_SUCCESS != GetKey(mAttr, cAttr, m_szKey))

{

strcpy(m_szKey, "NULL");

}

return m_szKey;

}

config.ini

[cctv.thrift]

IP=192.168.37.123

Port=7001

username=admin

password=admin

测试

static void ReadIniFile()

{

CIni  ini;

ini.OpenFile("config.ini", "r");

char *pVal1 = ini.GetStr("cctv.thrift", "IP");

int  nKey = ini.GetInt("cctv.thrift", "Port");

}

注意

GetStr返回的字符串,必须马上保存到其他的变量中,如果这个时候重新调用GetInt,上面的pVal1的值将会改变

时间: 2024-08-03 05:28:06

ini文件的读取的相关文章

C#对config.ini文件进行读取和修改

C#对config.ini文件进行读取和修改: public partial class Patrolcar : Form之后可以加入如下类: #region public class IniFile { public string map_length, map_width, maxnum_connect, net_ip; public string path; [DllImport("kernel32")] private static extern long WritePriva

C#读取ini文件的方法

最近项目用到ini文件,读取ini文件,方法如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.Specialized; using System.IO; using System.Runtime.InteropServices; using System.Windows.Forms; namespace test{ /

用C#读取,写入ini文件

[DllImport("kernel32.dll")] private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32.dll")] private static extern int GetPrivateProfileString(string section, st

VC关于INI文件的操作

1.查看INI文件是否存在,不存在则创建INI文件 //查看是否有First.ini文件 CFileFind fd; if(fd.FindFile(".First.int")) { MessageBox("找到文件"); } else { MessageBox("没找到文件,创建文件"); CFile file; file.Open(".\First.int",CFile::modeCreate|CFile::modeWrit

C++ 中使用boost::property_tree读取解析ini文件

boost 官网 http://www.boost.org/ 下载页面 http://sourceforge.net/projects/boost/files/boost/1.53.0/ 我下载的是 boost_1_53_0.tar.gz 使用系统  ubuntu 12.10 一.解压 [plain] view plaincopy tar -zxvf  boost_1_53_0.tar.gz 得到一个文件夹 boost_1_53_0,  拷贝其子目录 boost 到以下路径 [plain] vi

利用GetPrivateProfileString读取ini文件的字段

//INIClass读取类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.IO; using UnityEngine; namespace cReadConfigFile { public class INIClass { public string inipath; [

boost::property_tree读取解析ini文件--推荐

boost::property_tree读取解析ini文件 [cpp] view plaincopy #include "stdafx.h" #include <iostream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ini_parser.hpp> int main() { boost::property_tree::ptree pt; boos

C++ 读取INI文件

Windows操作系统专门为此提供了6个API函数来对配置设置文件进行读.写: GetPrivateProfileInt() 从私有初始化文件获取整型数值GetPrivateProfileString() 从私有初始化文件获取字符串型值GetProfileInt 从win.ini 获取整数值GetProfileString 从win.ini 获取字符串值WritePrivateProfileString 写字符串到私有初始化文件WriteProfileString 写字符串到win.ini 我们

Linux下读取Ini文件类

Linux下读取Ini文件类 最近项目上有需要读取Ini文件 所谓Ini文件也就是文本文档 并且以 //注释1 /*注释2 [Section] Key1=aaa Key2=bbb 这种形式存在的文档 自己编写了一个类  比较使用 简单 可以跨平台读写INI文件 头文件Ini.h #include <map> #include <string> using namespace std; #define CONFIGLEN 256 enum INI_RES { INI_SUCCESS,