遍历子目录下的文件名称并保存到变量vector 中
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <vector>
void scan_one_dir( const char * dir_name, vector<string> &file_names)
{
if(NULL == dir_name)
{
cout<<" dir_name is null ! "<<endl;
return;
}
struct stat s;
lstat( dir_name , &s );
if(!S_ISDIR(s.st_mode))
{
return;
}
struct dirent * filename;
DIR * dir;
dir = opendir(dir_name);
if(NULL == dir)
{
return;
}
int iName=0;
while((filename = readdir(dir)) != NULL)
{
if( strcmp(filename->d_name, "." ) == 0 || strcmp( filename->d_name , "..") == 0)
continue;
char wholePath[128] = {0};
sprintf(wholePath, "%s/%s", dir_name, filename->d_name);
string aaa;
aaa=wholePath;
aaa+="/20.jpg";
file_names.emplace_back(aaa);
}
}
int main()
{
vector<string> file_names;
scan_one_dir("../data/selected test data",file_names);
for (auto c : file_names)
{
cout << c << " ";
for (auto &ch : c)
{
ch = toupper(ch);
}
cout << "c"<<c << endl;
}
cout << endl;
}