Linux C++ 遍历子目录下的文件名称并保存到变量vector<string> 中

遍历子目录下的文件名称并保存到变量vector 中

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//列出一个目录下所有文件
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;
    }

你可能感兴趣的:(小技巧,linux)