递归文件夹扫描在c ++ [英] recursive folder scanning in c++

查看:147
本文介绍了递归文件夹扫描在c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好
我想扫描目录树并列出每个目录中的所有文件和文件夹。我创建了一个程序,从网络摄像头下载图像并将其保存在本地。此程序根据下载图片的时间创建文件树。我现在想要扫描这些文件夹并将图像上传到网络服务器,但我不知道如何扫描目录来找到图像。
如果任何人可以发布一些示例代码,这将是非常有帮助的。



编辑:我在嵌入式linux系统且不想使用boost

解决方案

请参阅 man ftw 。我还在此示例中使用了 fnmatch

  #include< ftw.h> 
#include< fnmatch.h>

static const char * filters [] = {
* .jpg,* .jpeg,* .gif,* .png
};

静态int回调(const char * fpath,const struct stat * sb,int typeflag){
/ *如果是文件* /
if(typeflag == FTW_F) {
int i;
/ *每个过滤器,* /
for(i = 0; i / *如果文件名匹配过滤器,* /
if(fnmatch(filters [i],fpath,FNM_CASEFOLD)== 0){
/ * do something * /
printf(found image:%s \\\
,fpath);
break;
}
}
}

/ *告诉ftw继续* /
return 0;
}

int main(){
ftw(。,callback,16);
}

(甚至没有经过编译测试, p>

这比处理 DIRENT s简单得多,可以递归遍历。






为了更好地控制遍历,还有 fts 。在此示例中,除非明确传递到程序作为起点,否则将跳过点文件(以。开头的文件和目录)。

  #include< fts.h> 
#include< string.h>

int main(int argc,char ** argv){
char * dot [] = {。,0};
char ** paths = argc> 1? argv + 1:dot;

FTS * tree = fts_open(paths,FTS_NOCHDIR,0);
if(!tree){
perror(fts_open);
return 1;
}

FTSENT * node;
while((node = fts_read(tree))){
if(node-> fts_level> 0&& node-> fts_name [0] =='。')
fts_set(tree,node,FTS_SKIP);
else if(node-> fts_info& FTS_F){
printf(获得在深度%d处命名为%s的文件,
可通过%s从当前目录
或通过从原始开始目录\\\

node-> fts_name,node-> fts_level,
node-> fts_accpath,node-> fts_path) ;
/ *如果fts_open没有给出FTS_NOCHDIR,
* fts可以改变程序的当前工作目录* /
}
}
if(errno){
perror(fts_read);
return 1;
}

if(fts_close(tree)){
perror(fts_close);
return 1;
}

return 0;
}

同样,它既不经过编译测试也不经过运行测试, 'd提及它。


Hi I want to scan a directory tree and list all files and folders inside each directory. I created a program that downloads images from a webcamera and saves them locally. This program creates a filetree based on the time the picture is downloaded. I now want to scan these folders and upload the images to a webserver but I´m not sure how I can scan the directories to find the images. If anyone could post some sample code it would be very helpful.

edit: I´m running this on an embedded linux system and don´t want to use boost

解决方案

See man ftw for a simple "file tree walk". I also used fnmatch in this example.

#include <ftw.h>
#include <fnmatch.h>

static const char *filters[] = {
    "*.jpg", "*.jpeg", "*.gif", "*.png"
};

static int callback(const char *fpath, const struct stat *sb, int typeflag) {
    /* if it's a file */
    if (typeflag == FTW_F) {
        int i;
        /* for each filter, */
        for (i = 0; i < sizeof(filters) / sizeof(filters[0]); i++) {
            /* if the filename matches the filter, */
            if (fnmatch(filters[i], fpath, FNM_CASEFOLD) == 0) {
                /* do something */
                printf("found image: %s\n", fpath);
                break;
            }
        }
    }

    /* tell ftw to continue */
    return 0;
}

int main() {
    ftw(".", callback, 16);
}

(Not even compile-tested, but you get the idea.)

This is much simpler than dealing with DIRENTs and recursive traversal yourself.


For greater control over traversal, there's also fts. In this example, dot-files (files and directories with names starting with ".") are skipped, unless explicitly passed to the program as a starting point.

#include <fts.h>
#include <string.h>

int main(int argc, char **argv) {
    char *dot[] = {".", 0};
    char **paths = argc > 1 ? argv + 1 : dot;

    FTS *tree = fts_open(paths, FTS_NOCHDIR, 0);
    if (!tree) {
        perror("fts_open");
        return 1;
    }

    FTSENT *node;
    while ((node = fts_read(tree))) {
        if (node->fts_level > 0 && node->fts_name[0] == '.')
            fts_set(tree, node, FTS_SKIP);
        else if (node->fts_info & FTS_F) {
            printf("got file named %s at depth %d, "
                "accessible via %s from the current directory "
                "or via %s from the original starting directory\n",
                node->fts_name, node->fts_level,
                node->fts_accpath, node->fts_path);
            /* if fts_open is not given FTS_NOCHDIR,
             * fts may change the program's current working directory */
        }
    }
    if (errno) {
        perror("fts_read");
        return 1;
    }

    if (fts_close(tree)) {
        perror("fts_close");
        return 1;
    }

    return 0;
}

Again, it's neither compile-tested nor run-tested, but I thought I'd mention it.

这篇关于递归文件夹扫描在c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆