#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string>
using namespace std;
void travel_dir(string path_name)
{
struct stat buf;
DIR *dir;
struct dirent *dir_ent;
if(stat(path_name.c_str(),&buf)==-1)
{
cout<<"stat error"<<endl;
return ;
}
if( !S_ISDIR(buf.st_mode))
{
cout<<path_name<<"is not dir"<<endl;
return;
}
if((dir = opendir(path_name.c_str())) == NULL)
{
cout<<"open dir"<<path_name<<"fail"<<endl;
return;
}
while((dir_ent = readdir(dir)) != NULL)
{
string ab_name = path_name;
ab_name += "/";
ab_name += dir_ent->d_name;
if(!strncmp(dir_ent->d_name,".",1))
{
continue;
}
if(stat(ab_name.c_str(),&buf) == -1)
{
cout<<ab_name<<"stat error"<<endl;
return;
}
if(S_ISREG(buf.st_mode))
{
cout<<ab_name<<endl;
}
if(S_ISDIR(buf.st_mode))
{
travel_dir(ab_name);
}
}
}
int main(int argc,char **argv)
{
travel_dir(argv[1]);
return 0;
}
遍历目录所有文件
最新推荐文章于 2024-05-09 10:14:45 发布