C 解析檔案

2021-09-26 21:02:29 字數 2089 閱讀 4015

最近自己寫個小東西遇到檔案解析的問題,從檔案中讀取所需要的特定值或字串,在網上找i了很多資料都不全,而且框架之間不相容,也不能跨平台,所以我就用了c++的檔案讀寫自己寫了乙個。

首先是思路:對檔案解析其實最後就是最字串解析的過程,所以長我好檔案讀寫和字串擷取問題不大。

推薦這兩篇文章:

首先來看我的檔案,需要在檔案中讀取括號中的點

下面貼出我的**:

首先是點**件,檔案解析多用於多執行緒,注意資源的保護,此處只是測試,並未加鎖,若有需要,可自己加鎖處理:

#pragma once

#include #include #include #include #include typedef struct _point_

tagpoint;

class cfileparse

;

.cpp檔案

#include "fileparse.h"  

cfileparse::cfileparse()

cfileparse::~cfileparse()

bool cfileparse::loadfile()

std::ifstream readfile(strfilepath);

//求檔案大小,利於充分利用空間

readfile.seekg(0, std::ios::end);//設定檔案指標到檔案流的尾部

std::streamoff ifilelen = readfile.tellg();

char *pfilechar = new char[(int)ifilelen +1];

readfile.seekg(0, 0);//設定檔案指標到檔案流的

readfile >> pfilechar;

m_strcontens.assign(pfilechar);//賦值

delete pfilechar;

return true;

}void cfileparse::parsefile(tagpoint &tag)

// std::string::size_type pos = m_strcontens.find_first_of(":"); //.find(s1);//查詢s中是否包含s1,並返回頭位置,找不到則返回string::npos

std::string::size_type pos = m_strcontens.find_first_of("(");

std::string::size_type newposx = m_strcontens.find_first_of(",");

std::string strinfox = m_strcontens.substr(pos+1, newposx - pos); //返回從pos開始newpos-pos個字元。

std::string::size_type newposy = m_strcontens.find_first_of(")");

std::string strinfoy = m_strcontens.substr(newposx+1,newposy-newposx);

tag.x = atoi(strinfox.c_str());

tag.y = atoi(strinfoy.c_str());

}bool cfileparse::adddata(char *pin)

m_vecfilepath.push_back(pin);

return true;

}bool cfileparse::getdata(char *pout)

char *p = m_vecfilepath.at(0);

strcpy_s(pout,256, p);

m_vecfilepath.erase(m_vecfilepath.begin());

return true;

}

main檔案

#include #include "fileparse.h"

int main()

C 解析XML檔案

內容提要 1.解析xml檔案有哪些方法?各有什麼優缺點?2.如何用xpath解析xml文件的要點。先來看看解析xml檔案的方法都有哪些吧,本段文字來自網路,可以幫助大家對這個問題有個概要的了解。在程式中訪問並操作xml檔案一般有兩種模型 流模型和dom 文件物件模型 流模型中有兩種變體 推 模型和 ...

C 解析 sln 檔案

我的專案,編碼工具 需要檢測開啟乙個工程,獲取所有專案。但是發現原來的方法,如果存在資料夾,把專案放在資料夾中,那麼是無法獲得專案,於是我就找了乙個方法去獲得sln檔案的所有專案。原先使用的方法dte.solution.projects但是放在資料夾的專案獲取不到,所以使用堆疊提供的方法。首先新增引...

C 解析XML檔案

我在這裡使用xmldocument通過using system.xml命名空間 xmldocument xml newxmldocument 先例項化 xmldocument xml.load path path可以為相對路徑或者為絕對路徑 xmlnodelist province xml.docu...