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