#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
void get_file_name_list(string path , list<string> &file_list)
{
int count = 0;
struct dirent* ent = NULL;
DIR *pDir;
char dir[512];
struct stat statbuf;
if ((pDir = opendir(path.data())) == NULL)
{
return;
}
while ((ent = readdir(pDir)) != NULL)
{
snprintf(dir, 512, "%s/%s", path.data(), ent->d_name);
lstat(dir, &statbuf);
if (!S_ISDIR(statbuf.st_mode))
{
count++;
}
}
closedir(pDir);
if ((pDir = opendir(path.data())) == NULL)
{
char log_buf[1024] = "0";
sprintf(log_buf,"Cannot open directory:%s\n", path.data());
printf("Cannot open directory:%s\n", path.data());
return;
}
int i;
for (i = 0; (ent = readdir(pDir)) != NULL && i < count;)
{
if (strlen(ent->d_name) <= 0)
{
continue;
}
snprintf(dir, 512, "%s/%s", path.data(), ent->d_name);
lstat(dir, &statbuf);
if (!S_ISDIR(statbuf.st_mode))
{
file_list.push_back(dir);
i++;
}
}
closedir(pDir);
}
读取指定目录下的所有文件。