linux 获取目录下的所有文件
#include#include#include#include#include#includevoid getFiles(char * dir, vector& files ){DIR *dp;struct dirent *entry;struct stat statbuf;if((dp = opendir(dir)) == NULL) {fpr
·
#include <stdio.h> #include <unistd.h> #include <string> #include <vector> #include <dirent.h> #include <sys/stat.h> void getFiles(char * dir, vector<string>& files ) { DIR *dp; struct dirent *entry; struct stat statbuf; if((dp = opendir(dir)) == NULL) { fprintf(stderr,"cannot open directory: %s\n", dir); return; } chdir(dir); char filename[1024]; while((entry = readdir(dp)) != NULL) { lstat(entry->d_name,&statbuf); if(S_ISDIR(statbuf.st_mode)) { if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0) continue; getFiles(entry->d_name,files); } else{ sprintf(filename,"%s/%s",dir,entry->d_name); files.push_back(filename); } } chdir(".."); closedir(dp); } int main(int argc,char * argv[]) { vector<string> files; getFiles(argv[1],files); int size = files.size(); for(int i =0;i < size;i++) printf("%s \n",files[i].c_str()); }
更多推荐
已为社区贡献1条内容
所有评论(0)