自动获取当前路径下的所有文件

主要是参考他的代码,写的不错,很受用!狠狠的复习了一把递归····

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(系统)
     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值