Доброго времени суток.
такая ситуация: есть дириктория с большим количеством файлов с буквально рандомными именами.
Вопрос есть ли в С++ какая то функция позволяющая внести все эти имена в указанной директорие внести в масив?
Код:
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdio.h>
#include <sys/types.h>
char *curdir;
int filter(const struct dirent *s)
{
char fullname[300];
struct stat st;
if(s->d_name[0]!='.'){
return 1;
}
return 0;
}
int main(int argc, char **argv){
struct dirent **e;
struct stat s;
struct tm *tmfile;
int i,dirsize,k;
char *empty[]={0,".",0};
char **list=argv;
char fullname[300];
if(argc==1)
list=empty,argc=2;
for (i = 1; i < argc; i++){
if(argc!=2)
curdir=list[i];
dirsize=scandir(list[i],&e,filter, 0);
if (dirsize<0){
printf("scandir fails at %s: %s\n",list[i],strerror(errno));
continue;
}
for(k=0;k<dirsize;k++){
sprintf(fullname,"%s/%s",list[i],e[k]->d_name);
if(stat(fullname, &s)==-1)
printf("stat fails at %s: %s\n",fullname,strerror(errno));
else{
printf ("%s ", e[k]->d_name);
}
}
}
}