c++实现逐行读取配置文件写入内存的示例

所属分类: 软件编程 / C 语言 阅读数: 85
收藏 0 赞 0 分享

不解析配置内容,只读取文件内容,剪去注释和首尾空格后写入缓存: vector<string> 中。供其他方法使用。
代码是在做一个MFC小工具时写的。

ReadProtocol.h

复制代码 代码如下:

/**
* 从文件中 读取 protocol 的内容 写入缓存
* 供外部方法使用
* Alex Liu, 2014
*/

#pragma once


#include <vector>
#include <map>
#include <list>
#include <string>

using namespace std;


#define MAX_FILEPATH 512

#define COMMENT_FLG '#'
#define SECTION_BEGIN_FLG '['
#define SECTION_END_FLG  ']'

class ReadProtocol {
public:
 ReadProtocol(char* FilePath);
 ~ReadProtocol();

 /**
 * 返回值为 errMsg 的地址 为了方便链式调用
 * 缺省返回 "成功"
 */
 char* GetErrInfo(char* errMsg, int errNo = 0);
 /**
 * 逐行读取文件内容 写入 m_StrVect
 * 使用 vector::push_back() 写入
 * return 0 成功 < 0 失败 可根据返回值 GetErrInfo
 */
 int ReadIniFile();

 /**
 * 获取根据目录获取一个
 * 使用 vector::push_back() 写入
 * return 0 成功 < 0 失败 可根据返回值 GetErrInfo
 */
 int GetOneSection(string Section, list<string> &Protocol);

private:
 void PushBackToVector(string oneLine);

private:
 char m_IniFile[MAX_FILEPATH];
 string m_ErrPos;

 map<string, unsigned int> m_SectionMap;
 vector<string> m_StrVect;
};

ReadProtocol.cpp

复制代码 代码如下:

//

//#include "stdafx.h"
#include <fstream>

#include "ReadProtocol.h"

//去掉字符串首尾的空格
static string strTrim(string aStr)
{
    string s = aStr;
    unsigned int first, last;
    if (string::npos != (first = s.find_first_not_of(' ') ))
        s = s.substr(first, s.length()-first);
    if (string::npos != (last = s.find_last_not_of(' ') ))
        s = s.substr(0, last+1);
    return s;
}

///=====================================================================================

ReadProtocol::ReadProtocol(char* FilePath)
{
 int iLen = (strlen(FilePath) > MAX_FILEPATH) ? MAX_FILEPATH : strlen(FilePath);
 memset(m_IniFile, 0, MAX_FILEPATH);
 memcpy(m_IniFile, FilePath, iLen);
}

ReadProtocol::~ReadProtocol()
{
 m_SectionMap.clear();
 m_StrVect.clear();
}

int ReadProtocol::GetOneSection(string Section, list<string> &Protocol)
{
 unsigned int Start = 0;
 // 注意这里不能使用 [] 运算符
 map<string, unsigned int>::iterator itr = m_SectionMap.find(Section);
 if (m_SectionMap.end() == itr)
 {
  m_ErrPos = Section;
  return -5;   // Unknown Section!!
 }
 else
 {
  Start = itr->second;
 }


 vector<string>::iterator it = m_StrVect.begin() + Start + 1;
 for (; it!=m_StrVect.end(); ++it)
 {
  unsigned int First, Last;
  First = it->find_first_of ( SECTION_BEGIN_FLG );
  Last = it->find_last_of ( SECTION_END_FLG );
  // stop when the next Section
  if( string::npos != First && string::npos != Last)
  {
   break;
  }

  Protocol.push_back(*it);
 }
 return (int)Protocol.size();
}

int ReadProtocol::ReadIniFile()
{
 ifstream fin(m_IniFile);
 if (!fin.is_open())
 {
  return -1; //can'topen file
 }
 string strLine;
 unsigned int Last;

 while (std::getline(fin, strLine).good())
 {
  if ( string::npos !=(Last = strLine.find_last_not_of('\r') ))
  {
   //delete \r
   strLine = strLine.substr(0, Last + 1);
  }
  PushBackToVector(strLine);
 }
 fin.close();

 if (m_StrVect.empty())
 {
  return -2; //get noting from file
 }
 return 0;
}

void ReadProtocol::PushBackToVector(string oneLine)
{
 unsigned int uPos;
 //去掉行尾注释
 if ( string::npos != (uPos = oneLine.find_first_of( COMMENT_FLG ) ) )
 {
  oneLine = oneLine.substr(0, uPos + 1);
 }
 //去首尾空格
 oneLine = strTrim(oneLine);
 if (oneLine.empty() || oneLine.length() < 2) return;

 //一行只能有一条记录
 unsigned int First, Last;
 First = oneLine.find_first_of(SECTION_BEGIN_FLG);
 Last = oneLine.find_last_of(SECTION_END_FLG);
 // is Section
 if( string::npos != First && string::npos != Last)
 {
  m_SectionMap[ oneLine.substr(First + 1, Last - First - 1) ] = m_StrVect.size();
 }
 m_StrVect.push_back(oneLine);
}

char* ReadProtocol::GetErrInfo(char* errMsg, int errNo)
{
 string errInfo;
 switch (errNo)
 {
 case 0:
  {
   errInfo = "Success!";
   break;
  }
 case -1:
  {
   char Path[1024] = {0};
   int pLength = 1024;
   GetCurrentDirectory(pLength, Path);
   errInfo.append("Can't open file. The file name is:==>\"");
   errInfo.append( m_IniFile);
   errInfo.append("\"\r\nMaybe no such file in Path:");
   errInfo.append(Path);
   break;
  }
 case -2:
  {
   errInfo = "Get noting from file: ";
   errInfo.append(m_IniFile);
   break;
  }
 case -3:
  {
   errInfo = "Analyze file failed. In ==> ";
   errInfo.append(m_ErrPos);
   break;
  }
 case -5:
  {
   errInfo = "\r\nUnknown Section!! ==> \"[";
   errInfo.append(m_ErrPos);
   errInfo.append("]\"\r\n请检查配置文件中是否有遗漏。");
   break;
  }
 default:
  {
   errInfo = "请按照正确步骤使用";
  }
 }
 memcpy(errMsg, errInfo.c_str(), errInfo.length());
 return errMsg;
}

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多