自動獲取當前路徑下的所有文件

主要是參考他的代碼,寫的不錯,很受用!狠狠的複習了一把遞歸····

https://www.cnblogs.com/fnlingnzb-learner/p/6424563.html


# include<iostream>
# include<io.h>
# include<string>
# include<direct.h>
# include<vector>

using namespace std;
void getFiles(string path, vector<string>& files);
void get_files(string path, vector<string>& files);
int main()

{
	//獲取當前路徑:
	char path1[1024];
	getcwd(path1, 1024);
	string path = path1;

	printf("當前路徑爲:%s\n", path.c_str());
	//查找下級目錄和當前文件夾文件名稱
	vector<string> files;
	get_files(path, files);
	for (auto &ele : files)
	{
		cout << endl;
		printf("%s", ele.c_str());
	}
	getchar();
	return 0;
}
void get_files(string path, vector<string>& files)
{
	struct _finddata_t document;
	string p;
	intptr_t index = 0;
	if ((index = _findfirst(p.assign(path).append("\\*").c_str(), &document)) != -1)
	{
		do
		{
			if ((document.attrib & _A_SUBDIR))
			{
				if (strcmp(document.name, ".") != 0 && strcmp(document.name,"..")!=0)
				{
					get_files(p.assign(path).append("\\").append(document.name), files);
				}
			}
			else
			{
				if (strcmp(document.name, ".") != 0 && strcmp(document.name, "..") != 0)
				{
					files.push_back(p.assign(path).append("\\").append(document.name));
				}
			}

		} while (!_findnext(index,&document));
		_findclose(index);
	}
	
}

unsigned atrrib:
  文件屬性的存儲位置。它存儲一個unsigned單元,用於表示文件的屬性。文件屬性是用位表示的,主要有以下一些:
    _A_ARCH(存檔)、       _A_HIDDEN(隱藏)、_A_NORMAL(正常)、

      _A_SUBDIR(文件夾)、_A_RDONLY(只讀)、_A_SYSTEM(系統)
     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章