c 遍历文件夹下所有图片文件

#include <iostream>
#include <fstream>

#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <string>

#define INPUTS_PATH "./inputs/case1"

int main(void)
{
    DIR *d = NULL;
    struct dirent *dp = NULL;
    struct stat st;

    if (stat(INPUTS_PATH, &st) < 0 || !S_ISDIR(st.st_mode))
    {
        std::cout << "invalid path: " << INPUTS_PATH << std::endl;
        return 0;
    }

    if (!(d = opendir(INPUTS_PATH)))
    {
        std::cout << "opendir error: " << INPUTS_PATH << std::endl;
        return 0;
    }

    while ((dp = readdir(d)) != NULL)
    {
        if ((!strncmp(dp->d_name, ".", 1)) || (!strncmp(dp->d_name, "..", 2)))
            continue;
        std::string file_name;
        file_name = std::string(INPUTS_PATH) + std::string(dp->d_name);
        stat(file_name.c_str(), &st);

        // 后缀检查
        // std::cout << (strstr(file_name.c_str(), ".cimg") != 0) << std::endl;
        if (strstr(file_name.c_str(), ".cimg"))
        {
            std::cout << file_name << std::endl;
        }
    }
    return 0;
}

 

posted @ 2022-08-23 14:56  Wangtn  阅读(163)  评论(0编辑  收藏  举报