c++實(shí)現(xiàn)逐行讀取配置文件寫(xiě)入內(nèi)存的示例
不解析配置內(nèi)容,只讀取文件內(nèi)容,剪去注釋和首尾空格后寫(xiě)入緩存: vector<string> 中。供其他方法使用。
代碼是在做一個(gè)MFC小工具時(shí)寫(xiě)的。
ReadProtocol.h
/**
* 從文件中 讀取 protocol 的內(nèi)容 寫(xiě)入緩存
* 供外部方法使用
* 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)容 寫(xiě)入 m_StrVect
* 使用 vector::push_back() 寫(xiě)入
* return 0 成功 < 0 失敗 可根據(jù)返回值 GetErrInfo
*/
int ReadIniFile();
/**
* 獲取根據(jù)目錄獲取一個(gè)
* 使用 vector::push_back() 寫(xiě)入
* 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
//
//#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請(qǐng)檢查配置文件中是否有遺漏。");
break;
}
default:
{
errInfo = "請(qǐng)按照正確步驟使用";
}
}
memcpy(errMsg, errInfo.c_str(), errInfo.length());
return errMsg;
}
上一篇:C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
欄 目:C語(yǔ)言
本文標(biāo)題:c++實(shí)現(xiàn)逐行讀取配置文件寫(xiě)入內(nèi)存的示例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3655.html
您可能感興趣的文章
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見(jiàn)的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語(yǔ)言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置