簡體   English   中英

C ++中的遞歸文件夾掃描

[英]recursive folder scanning in c++

我想掃描目錄樹並列出每個目錄中的所有文件和文件夾。 我創建了一個程序,可從網絡攝像機下載圖像並將其保存在本地。 該程序根據圖片的下載時間創建文件樹。 我現在想掃描這些文件夾並將圖像上傳到Web服務器,但是我不確定如何掃描目錄以查找圖像。 如果有人可以張貼一些示例代碼,那將非常有幫助。

編輯 :我在嵌入式linux系統上運行它,不想使用boost

有關簡單的“文件樹遍歷”,請參見man ftw 在此示例中,我還使用了fnmatch

#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);
}

(甚至沒有經過編譯測試,但是您知道了。)

這比自己處理DIRENT和遞歸遍歷要簡單得多。


為了更好地控制遍歷,還可以使用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("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;
}

同樣,它既沒有經過編譯測試也沒有運行測試,但我想我已經提到了。

Boost.Filesystem允許您執行此操作。 查看文檔

編輯:
如果您使用的是Linux,並且不想使用Boost,則必須使用Linux本機C函數。 此頁面顯示了有關如何執行此操作的許多示例。

我是老學校,對我沒有ftw()! 這很粗糙(自從我進行直接C編程以來已經有一段時間了),並且很多東西都是硬編碼的,而且我可能弄亂了strnc *()函數的長度計算,但是您明白了。 K&R btw中有一個類似的例子。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <dirent.h>

void listdir(char* dirname, int lvl);

int main(int argc, char** argv)
{

  if (argc != 2) {
    fprintf(stderr, "Incorrect usage!\n");
    exit(-1);
  }
  listdir(argv[1], 0);


  return 0;
}

void listdir(char* dirname, int lvl)
{

  int i;
  DIR* d_fh;
  struct dirent* entry;
  char longest_name[4096];

  while( (d_fh = opendir(dirname)) == NULL) {
    fprintf(stderr, "Couldn't open directory: %s\n", dirname);
    exit(-1);
  }

  while((entry=readdir(d_fh)) != NULL) {

    /* Don't descend up the tree or include the current directory */
    if(strncmp(entry->d_name, "..", 2) != 0 &&
       strncmp(entry->d_name, ".", 1) != 0) {

      /* If it's a directory print it's name and recurse into it */
      if (entry->d_type == DT_DIR) {
        for(i=0; i < 2*lvl; i++) {
          printf(" ");
        }
        printf("%s (d)\n", entry->d_name);

        /* Prepend the current directory and recurse */
        strncpy(longest_name, dirname, 4095);
        strncat(longest_name, "/", 4095);
        strncat(longest_name, entry->d_name, 4095);
        listdir(longest_name, lvl+1);
      }
      else {

        /* Print some leading space depending on the directory level */
        for(i=0; i < 2*lvl; i++) {
          printf(" ");
        }
        printf("%s\n", entry->d_name);
      }
    }
  }

  closedir(d_fh);

  return;
}

您將要使用dirent.h中聲明的目錄功能。 Wikipedia頁面描述了它們並包括示例代碼。 對於您的應用程序,一旦確定了目錄,您將希望再次遞歸調用處理函數以處理目錄內容。

您也可以使用glob / globfree。

我認為,如果可以使用Qt / Embedded,則有QDir和QFileInfo類可以為您提供幫助,盡管這取決於您是否可以使用Qt。 問題是您的系統提供哪種API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM