日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C++提取文件名與提取XML文件的方法詳解_C 語言

作者:deepython ? 更新時(shí)間: 2022-05-06 編程語言

1、提取文件名

  • 查找容器內(nèi)子序列的最后一次出現(xiàn)的位置

std::find_end(str.begin(), str.end(), pattern.begin(), pattern.end())

  • 查找容器內(nèi)子序列的第一次出現(xiàn)的位置

std::search()

  • find函數(shù)主要實(shí)現(xiàn)的是在容器內(nèi)查找指定的元素,并且這個(gè)元素必須是基本數(shù)據(jù)類型的。查找成功返回一個(gè)指向指定元素的迭代器,查找失敗返回end迭代器。

std::find()

  • 返回兩個(gè)迭代器之間的距離,也可以理解為計(jì)算兩個(gè)元素 first 和 last 之間的元素?cái)?shù)

std::distance(str.begin(), result)

  • substr()截取字符串子序列,第一個(gè)參數(shù)為開始索引,第二參數(shù)是子序列長度
  • substring() 截取字符串子序列,第一個(gè)參數(shù)為開始索引,第二參數(shù)是結(jié)束索引
  • str.substr(0, std::distance(str.begin(), result) + 1)
#include 
#include 
# include   //注意要包含該頭文件
using namespace std;
std::string ExtractFileName(std::string path)
{
   //不帶后綴名的文件名
   std::string fileBaseName;
   //文件目錄
   std::string str = path;
   //待匹配的子序列
   std::string pattern = "/";
   //查找容器內(nèi)子序列的最后一次出現(xiàn)的位置,在[str.begin(),str.end ())內(nèi)搜索由[pattern.begin(), pattern.end())
   //組成的子序列,然后將迭代器返回到其第一個(gè)元素,即pattern.begin(),若沒有發(fā)現(xiàn),返回-1
   // 與std::search()類似,后者返回子序列第一次出現(xiàn)的位置
   auto result = std::find_end(str.begin(), str.end(), pattern.begin(), pattern.end());
   if (result != str.end())
   {
   	//substr()截取字符串子序列,第一個(gè)參數(shù)為開始索引,第二參數(shù)是子序列長度
   	//substring(截取字符串子序列,第一個(gè)參數(shù)為開始索引,第二參數(shù)是結(jié)束索引
   	//目錄
   	auto dirName = str.substr(0, std::distance(str.begin(), result) + 1);
   	//帶后綴名的文件名
   	auto fileName = str.substr(std::distance(str.begin(), result) + 1);
   	//不帶后綴名的文件名
   	fileBaseName = fileName.substr(0, fileName.size() - 4);
   }
   return fileBaseName;
}

2、提取XML文件

首先要引入tinyxml2的頭文件,tinyxml2.h和tinyxml2.cpp

xml文件內(nèi)容:

MD5123

聲明XMLDocument變量,存放xml文件

tinyxml2::XMLDocument doc

讀取xml文件

doc.LoadFile("demo.xml")

獲取頭節(jié)點(diǎn)

XMLElement *root = doc.RootElement();

頭結(jié)點(diǎn)的兄弟節(jié)點(diǎn)

XMLElement *root1 = root->NextSiblingElement()

獲取節(jié)點(diǎn)的id的屬性

root1->Attribute("id");

獲取節(jié)點(diǎn)的name的屬性

head->Attribute("name")

獲取節(jié)點(diǎn)的文本內(nèi)容

root1->GetText();

獲取頭結(jié)點(diǎn)下的head節(jié)點(diǎn)

XMLElement *head = root->FirstChildElement("head")
#include 
#include 
#include 
#include 
#include "tinyxml2-master/tinyxml2.h"
using namespace std;
using namespace tinyxml2;
void readXML()
{
//聲明XMLDocument變量
	tinyxml2::XMLDocument doc;
	//讀取xml文件
	doc.LoadFile("demo.xml");
	//判斷是否讀取成功
	if (doc.Error())
	{
		printf("Load XML failed!");
		return;
	}
	//獲取頭節(jié)點(diǎn)
	XMLElement *root = doc.RootElement();
//判斷頭結(jié)點(diǎn)有沒有兄弟節(jié)點(diǎn) 
	if (root->NextSiblingElement() != NULL)
	{
		//頭結(jié)點(diǎn)的兄弟節(jié)點(diǎn)
		XMLElement *root1 = root->NextSiblingElement();
		//獲取節(jié)點(diǎn)的id的屬性
		printf("第二個(gè)一級節(jié)點(diǎn)%s\n", root1->Attribute("id"));
	}
	if (root->GetText() != NULL)
	{
		string rootStr = root->GetText();
		printf("第一個(gè)一級節(jié)點(diǎn)的內(nèi)容%s\n", rootStr);
	}
	XMLElement *head = root->FirstChildElement("head");
	//獲取節(jié)點(diǎn)的內(nèi)容
	printf("head的內(nèi)容%s\n", head->GetText());
	printf("head的id%s\n", head->Attribute("id"));
	printf("head的name%s\n", head->Attribute("name"));
	system("pause");

總結(jié)

今天用C++實(shí)現(xiàn)了提取文件名與XML文件。

原文鏈接:https://blog.csdn.net/weixin_44095417/article/details/123296235

欄目分類
最近更新