7/04/2018

get files list in a directory in ubuntu

refer to below code:

..
std::vector<std::string> list_files_in_dir(const char* dirPath)
        {
    DIR *dir;

    std::vector<std::string> files;

    struct dirent *ent;
    if ((dir = opendir (dirPath)) != NULL)
    {
        /* print all the files and directories within directory */
        while ((ent = readdir (dir)) != NULL)
        {
            if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0)
            {
                stringstream fullpath;
                fullpath << dirPath << "/" << ent->d_name;
                files.push_back(fullpath.str());
            }
        }
        closedir (dir);
    }
    else
    {
        /* could not open directory */
        perror ("");
        return files;
    }

    return files;
}
..

No comments:

Post a Comment