cpp list files end with suffix and list them respectively

#include <chrono>
#include <ctime>
#include <dirent.h>
#include <fstream>
#include <iostream>
#include <thread>
#include <unistd.h>
#include <uuid/uuid.h>
#include <vector>

void findTxtFilesInVector(string path)
{
    vector<string> vec;
    dirent *dp;
    DIR *dir;
    dir=opendir(path.c_str());
    if(dir==nullptr)
    {
        cout<<"Open  directory "<<path<<" failed!"<<endl<<endl;
        return;
    }

    while((dp=readdir(dir))!=nullptr)
    {
        if(dp->d_type==DT_REG)
        {
            string fileName(dp->d_name);
            if(fileName.find(".txt")!=string::npos)
            {
                vec.push_back(dp->d_name); 
            }
        }
    }
    cout<<"There are "<<vec.size()<<" text files!!!"<<endl<<endl<<endl;

    for (const auto &i : vec)
    {
        cout<<i<<endl;
    }
    
    cout<<endl<<endl<<"Finished in "<<__FUNCTION__<<endl;
}

 

 

posted @ 2022-12-03 19:19  FredGrit  阅读(15)  评论(0编辑  收藏  举报