#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h> // /usr/include/dirent.h
#include<string.h>
#include<iostream>
#include <vector>
using namespace std;
int getAbsoluteFiles(string directory, vector<string>& filesAbsolutePath);
int main()
{
vector<string> test;
int status = getAbsoluteFiles("./images/", test);
cout<<" test.size: " << test.size() <<endl;
}
int getAbsoluteFiles(string directory, vector<string>& filesAbsolutePath) //参数1[in]要变量的目录 参数2[out]存储文件名
{
DIR* dir = opendir(directory.c_str()); //打开目录 DIR-->类似目录句柄的东西
if ( dir == NULL )
{
cout<<directory<<" is not a directory or not exist!"<<endl;
return -1;
}struct dirent* d_ent = NULL; //dirent-->会存储文件的各种属性
char fullpath[128] = {0};
char dot[3] = "."; //linux每个下面都有一个 . 和 .. 要把这两个都去掉
char dotdot[6] = "..";while ( (d_ent = readdir(dir)) != NULL ) //一行一行的读目录下的东西,这个东西的属性放到dirent的变量中
{
if ( (strcmp(d_ent->d_name, dot) != 0)
&& (strcmp(d_ent->d_name, dotdot) != 0) ) //忽略 . 和 ..
{
if ( d_ent->d_type == DT_DIR ) //d_type可以看到当前的东西的类型,DT_DIR代表当前都到的是目录,在usr/include/dirent.h中定义的
{string newDirectory = directory + string("/") + string(d_ent->d_name); //d_name中存储了子目录的名字
if( directory[directory.length()-1] == '/')
{
newDirectory = directory + string(d_ent->d_name);
}if ( -1 == getAbsoluteFiles(newDirectory, filesAbsolutePath) ) //递归子目录
{
return -1;
}
}
else //如果不是目录
{
string absolutePath = directory + string("/") + string(d_ent->d_name); //构建绝对路径
if( directory[directory.length()-1] == '/') //如果传入的目录最后是/--> 例如a/b/ 那么后面直接链接文件名
{
absolutePath = directory + string(d_ent->d_name); // /a/b/1.txt
}
filesAbsolutePath.push_back(absolutePath);
}
}
}closedir(dir);
return 0;
}
linux 遍历文件夹下所有文件
最新推荐文章于 2024-07-26 13:39:51 发布