linux c 读取并处理文件夹下的所有文件

3 篇文章 0 订阅

定义传递参数使用的结构体:头文件<param.h>

#ifndef _PARAM_H_
#define _PARAM_H_

typedef struct str_1_param
{
    char *str;
    void *param;
} STR_1_PARAM, *pSTR_1_PARAM;

#endif

遍历并处理每个文件(指针函数作为参数传递进来现,具体负责处理文件): <procdir.h>

#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include "param.h"
//#include <unistd.h>

int procdir(char *base, void (*p)(void*), void *param, int isRecursive)
{
    struct dirent *entry = NULL, **namelist;
    char path[1000];

    int num;
    num = scandir(base, &namelist, NULL, alphasort);
    if (num<0)
    {
    
        printf("Open dir %s failed!\n", base);
        return -1;
    }
    else
    {
        int i;
        for(i=0;i<num;i++)
        {
            entry = namelist[i]; 
            if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") ==0)
            {
                continue;
            }
            else if(entry->d_type == 8) //file
            {
                //process the file
                memset(path, '\0', sizeof(path));
                sprintf(path, "%s/%s", base, entry->d_name);
                ((STR_1_PARAM*)param)->str = path;
                (*p)(param);
            } 
            else if(isRecursive && entry->d_type == 4) //directory
            {
                //process dir
                memset(path, '\0', sizeof(path));
                sprintf(path, "%s/%s", base, entry->d_name);
                procdir(path, p, param, isRecursive);
            }
        }
    }
    return 1;
}

使用方法:遍历文件夹下图片,提起hog特征:

#include <stdio.h>
#include <getopt.h>
#include "param.h"
#include "procdir.h"
#include "cv.h"
#include "opencv2/opencv.hpp"

using namespace cv;
 
typedef struct MY_PARAM
{
    IplImage *img;
    IplImage *dst;
    HOGDescriptor *hog;
    FILE *pout;
    vector<float> descriptors;
} MY_PARAM, *pMY_PARAM;

void extr_hog(void *s1p)
{
    STR_1_PARAM *str1param = (STR_1_PARAM*)s1p;
    MY_PARAM *my_param = (MY_PARAM*)str1param->param;
    my_param->img = cvLoadImage(str1param->str,0);
    cvSmooth(my_param->img, my_param->dst, CV_GAUSSIAN, 3, 3);
    my_param->hog->compute(my_param->dst, my_param->descriptors, Size(1,1), Size(0,0));
    for(int i=0;i<my_param->descriptors.size();i++)
    {
        fprintf(my_param->pout, "%f ", my_param->descriptors[i]);
    }
    fprintf(my_param->pout, "\n");
    cvReleaseImage(&my_param->img);
}

int main(int argc, char **argv)
{
    if(argc != 5 )
    {
        printf("Usage:\n%s [-d InputBaseDir] [-o OutputFile]\n", argv[0]);
        return -1;
    }

    char tmp, *base, *output;
    while((tmp=getopt(argc,argv,"d:o:"))!=-1)
    {
        switch(tmp)
        {
            case 'd':
                base = optarg;
                break;
            case 'o':
                output = optarg;
                break;
        }
    }

    FILE *pOutFile;
    pOutFile =  fopen(output,"w");
    if(!pOutFile)
    {
        printf("Open/create file %s failed!\n", output);
        return -1;
    }

    MY_PARAM my_param;
    my_param.dst = cvCreateImage(cvSize(64,64),8,1);
    my_param.pout = pOutFile;
    my_param.hog = new HOGDescriptor(cvSize(64,64),cvSize(16,16),cvSize(8,8),cvSize(8,8),9);
    STR_1_PARAM str1param;
    str1param.param = &my_param;
    
    int rval = procdir(base, extr_hog, &str1param, 1);

    fclose(pOutFile);

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现这个功能,需要先获取文件夹中所有文件的创建时间,然后比较它们的创建时间,找到最新的文件。 下面是一个简单的 C 代码示例,可以读取指定文件夹下的所有文件,找到最新的文件并输出它的名称: ```c #include <stdio.h> #include <string.h> #include <dirent.h> #include <sys/stat.h> int main() { char folder_path[100] = "path/to/folder"; // 文件夹路径 DIR *dir; struct dirent *ent; struct stat st; time_t latest_create_time = 0; char latest_file_name[100]; if ((dir = opendir(folder_path)) != NULL) { while ((ent = readdir(dir)) != NULL) { char file_path[100]; sprintf(file_path, "%s/%s", folder_path, ent->d_name); if (stat(file_path, &st) == 0) { time_t create_time = st.st_ctime; if (create_time > latest_create_time) { latest_create_time = create_time; strcpy(latest_file_name, ent->d_name); } } } closedir(dir); } else { printf("Failed to open folder!"); return 1; } printf("The latest file in the folder is %s\n", latest_file_name); return 0; } ``` 在代码中,我们使用了 `opendir()` 函数打开文件夹,然后使用 `readdir()` 函数遍历文件夹中的所有文件。对于每个文件,我们使用 `stat()` 函数获取它的属性信息,包括创建时间 `st_ctime`。然后,我们比较这些时间,找到最新的文件。 需要注意的是,上述代码只能在 Linux 或者类 Unix 系统中运行。如果要在 Windows 系统中运行,需要使用不同的文件路径表示方式,并且可能需要使用 `_stat()` 函数代替 `stat()` 函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值