欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c++實(shí)現(xiàn)逐行讀取配置文件寫入內(nèi)存的示例

 更新時(shí)間:2014年05月06日 09:18:44   作者:  
這篇文章主要介紹了c++實(shí)現(xiàn)逐行讀取配置文件寫入內(nèi)存的示例,需要的朋友可以參考下

不解析配置內(nèi)容,只讀取文件內(nèi)容,剪去注釋和首尾空格后寫入緩存: vector<string> 中。供其他方法使用。
代碼是在做一個(gè)MFC小工具時(shí)寫的。

ReadProtocol.h

復(fù)制代碼 代碼如下:

/**
* 從文件中 讀取 protocol 的內(nèi)容 寫入緩存
* 供外部方法使用
* 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 的地址 為了方便鏈?zhǔn)秸{(diào)用
 * 缺省返回 "成功"
 */
 char* GetErrInfo(char* errMsg, int errNo = 0);
 /**
 * 逐行讀取文件內(nèi)容 寫入 m_StrVect
 * 使用 vector::push_back() 寫入
 * return 0 成功 < 0 失敗 可根據(jù)返回值 GetErrInfo
 */
 int ReadIniFile();

 /**
 * 獲取根據(jù)目錄獲取一個(gè)
 * 使用 vector::push_back() 寫入
 * return 0 成功 < 0 失敗 可根據(jù)返回值 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

復(fù)制代碼 代碼如下:

//

//#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;
 // 注意這里不能使用 [] 運(yùn)算符
 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;
}

相關(guān)文章

  • C語言實(shí)現(xiàn)單鏈表反轉(zhuǎn)

    C語言實(shí)現(xiàn)單鏈表反轉(zhuǎn)

    這篇文章主要介紹了C語言實(shí)現(xiàn)單鏈表反轉(zhuǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • C語言中字符串處理函數(shù)sscanf的用法

    C語言中字符串處理函數(shù)sscanf的用法

    一直對于一些日期字符串中數(shù)字的提取比較頭疼,現(xiàn)看到 sscanf 對于字符串中的內(nèi)容提取較方便,本文主要介紹了C語言中字符串處理函數(shù)sscanf的用法,具有一定參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • C++11新特性“=default”,“=delete”的使用

    C++11新特性“=default”,“=delete”的使用

    =default、=delete 是C++11的新特性,分別為:顯式缺省(告知編譯器生成函數(shù)默認(rèn)的缺省版本)和顯式刪除(告知編譯器不生成函數(shù)默認(rèn)的缺省版本),本文就來介紹一下如何使用
    2021-05-05
  • C語言實(shí)現(xiàn)三子棋小游戲的示例代碼

    C語言實(shí)現(xiàn)三子棋小游戲的示例代碼

    這篇文章主要介紹了如何通過C語言實(shí)現(xiàn)三子棋小游戲,三子棋小游戲的實(shí)現(xiàn)主要依賴于循環(huán)語句、函數(shù)和數(shù)組,感興趣的小伙伴可以嘗試一下
    2022-10-10
  • QT5實(shí)現(xiàn)TTS文本語音朗讀功能

    QT5實(shí)現(xiàn)TTS文本語音朗讀功能

    TTS?語音朗讀?是開發(fā)中常用的功能,Qt已經(jīng)給封裝完成,我們只需要調(diào)用即可,本文就為大家介紹了QT5如何調(diào)用實(shí)現(xiàn)文本朗讀功能的,需要的可以參考一下
    2023-05-05
  • C++和java設(shè)計(jì)模式之單例模式

    C++和java設(shè)計(jì)模式之單例模式

    這篇文章主要為大家詳細(xì)介紹了C++和java設(shè)計(jì)模式之單例模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • C++使用鏈表實(shí)現(xiàn)圖書管理系統(tǒng)

    C++使用鏈表實(shí)現(xiàn)圖書管理系統(tǒng)

    這篇文章主要介紹了C++使用鏈表實(shí)現(xiàn)圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • MySQL的內(nèi)存表的基礎(chǔ)學(xué)習(xí)教程

    MySQL的內(nèi)存表的基礎(chǔ)學(xué)習(xí)教程

    這篇文章主要介紹了MySQL的內(nèi)存表的基礎(chǔ)學(xué)習(xí)教程,包括內(nèi)存表的創(chuàng)建以及使用限制等等,需要的朋友可以參考下
    2015-12-12
  • C/C++中的static關(guān)鍵字詳解

    C/C++中的static關(guān)鍵字詳解

    這篇文章主要為大家詳細(xì)介紹了 C/C++中的static關(guān)鍵字,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • vscode編譯運(yùn)行c語言報(bào)錯(cuò)亂碼的解決

    vscode編譯運(yùn)行c語言報(bào)錯(cuò)亂碼的解決

    本文主要介紹了vscode編譯運(yùn)行c語言報(bào)錯(cuò)亂碼,文中通過圖文介紹的的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07

最新評論