1. 程式人生 > >VS2015獲取某個特定資料夾下的所有XML檔案/TXT檔案

VS2015獲取某個特定資料夾下的所有XML檔案/TXT檔案

測試案例

#include<iostream>
#include<string>
#include<vector>
#include<io.h>
#include<tinystr.h>
#include<tinyxml.h>

using namespace std;

//box引數結構體
struct BoxSize
{
	int xMin;
	int yMin;
	int xMax;
	int yMax;
};

int main(int argc, char* argv[]) {	
	string m_strXmlPath = "E:\\eye_data\\labels";
	long hFile = 0;
	struct _finddata_t fileInfo;
	string pathName, exdName;
	// \\* 代表要遍歷所有的型別
	if ((hFile = _findfirst(pathName.assign(m_strXmlPath).append("\\*").c_str(), &fileInfo)) == -1)
	{
		return -1;
	}
	do
	{
		//判斷檔案的屬性是資料夾還是檔案
		//cout << fileInfo.name << (fileInfo.attrib&_A_SUBDIR ? "[folder]" : "[file]") << endl;
		if (fileInfo.attrib&_A_SUBDIR)
			continue;
		TiXmlDocument Document;
		//讀取xml檔案中的引數值
		string path = m_strXmlPath + "\\" + (string)fileInfo.name;
		//cout << path << endl;
		if (!Document.LoadFile(path.c_str()))
		{
			cout << "無法載入xml檔案!" << endl;
			return -1;
		}
		else
			cout << "success" << endl;
		TiXmlElement* RootElement = Document.RootElement();		//根目錄
		TiXmlElement* NextElement = RootElement->FirstChildElement();		//根目錄下的第一個節點層
		while (NextElement != NULL)		//判斷有沒有讀完
		{
			if (NextElement->ValueTStr() == "filename")		//讀到filename節點
			{
				cout << NextElement->GetText() << endl;
				break;
			}
			NextElement = NextElement->NextSiblingElement();
		}
	} while (_findnext(hFile, &fileInfo) == 0);
	_findclose(hFile);
	return 0;
}

執行結果

在這裡插入圖片描述