获取指定目录的所有文件

在指定的目录下,要获取其目录下所有的文件,包括其所有的子目录,子目录的子目录。 主要思想: 将指定目录放入一个队列中,出队列,遍历其下面的文件,若为文件,则加入列表,若为目录,加入到后续队列中,重复操作,直到队列为空。

FileList是文件列表,Dire是指定的目录

int GetSigFileList1(vector<string> &FileList, std::string &Dire)
{
	queue<string> DirQueue;
	string DirName="/tmp/test/"+Dire;
	string FilePath = "";
	
	DIR *dir;
	if (!(dir = opendir(DirName.c_str())))
	{
		return 0;
	}
	
	DirQueue.push(DirName.c_str());
	
	struct dirent *Ent;
	
	while (!DirQueue.empty())
	{
		DirName = DirQueue.front();
		dir = opendir(DirName.c_str());
		DirQueue.pop();
		if (!dir)
		{
			continue;
		}
		
		while (Ent = readdir(dir))
		{
			if (strcmp(".",Ent->d_name) == 0 || strcmp("..", Ent->d_name) == 0)
			{
				continue;
			}
			if (Ent->d_type == DT_DIR)
			{
				DirQueue.push(DirName + Ent->d_name + "/");
			}
			if (Ent->d_type == DT_REG)
			{
				FilePath = DirName.substr(10, strlen(DirName.c_str()));
				FileList.push_back(FilePath + Ent->d_name);
			}		
		}
		closedir(dir);		
	}
	return 0;
}

转载于:https://my.oschina.net/lvguidong/blog/863295

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值