C++实现类似python os.listdir功能

#include <string>
#include <dirent.h>


std::vector<std::string>readFolder(const std::string &image_path)
{
    std::vector<std::string> image_names;
    auto dir = opendir(image_path.c_str());

    if ((dir) != NULL)
    {
        struct dirent *entry;
        entry = readdir(dir);
        while (entry)
        {
            auto temp = image_path + entry->d_name;
            if (strcmp(entry->d_name, "") == 0 || strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
            {
                entry = readdir(dir);
                continue;
            }
            std::cout << image_path + "/" + entry->d_name << std::endl;
            image_names.push_back(temp);
            entry = readdir(dir);
        }
    }
    return image_names;
}

猜你喜欢

转载自blog.csdn.net/linghu8812/article/details/104983559