2015-05-14 2 views
-1

Как написать программу, которая входит во все папки «подпапки»?
Я написал код, но он не входит в подпапки.Ввод всех подпапок - Рекурсивный

void main(int argc, char *argv[]) 
{ 
char* dirPath = argv[1]; 
struct stat statbuf; 
DIR *dir; 
struct dirent *ent; 
size_t arglen = strlen(argv[1]); 

if ((dir = opendir (dirPath)) != NULL) { 
    while ((ent = readdir (dir)) != NULL) { 
     printf(ent->d_name, "%s\n"); 
    } 
    closedir (dir); 
} else { 
    perror ("Problem"); 
    } 
} 

Я попытался использовать функцию stat() рекурсивно.

ответ

0

http://www.lemoda.net/c/recursive-directory/

#include <stdlib.h> 
#include <stdio.h> 
#include <sys/types.h> 
#include <string.h> 
#include <errno.h> 
/* "readdir" etc. are defined here. */ 
#include <dirent.h> 
/* limits.h defines "PATH_MAX". */ 
#include <limits.h> 

/* List the files in "dir_name". */ 

static void 
list_dir (const char * dir_name) 
{ 
    DIR * d; 

    /* Open the directory specified by "dir_name". */ 

    d = opendir (dir_name); 

    /* Check it was opened. */ 
    if (! d) { 
     fprintf (stderr, "Cannot open directory '%s': %s\n", 
       dir_name, strerror (errno)); 
     exit (EXIT_FAILURE); 
    } 
    while (1) { 
     struct dirent * entry; 
     const char * d_name; 

     /* "Readdir" gets subsequent entries from "d". */ 
     entry = readdir (d); 
     if (! entry) { 
      /* There are no more entries in this directory, so break 
       out of the while loop. */ 
      break; 
     } 
     d_name = entry->d_name; 
     /* Print the name of the file and directory. */ 
    printf ("%s/%s\n", dir_name, d_name); 

#if 0 
    /* If you don't want to print the directories, use the 
     following line: */ 

     if (! (entry->d_type & DT_DIR)) { 
     printf ("%s/%s\n", dir_name, d_name); 
    } 

#endif /* 0 */ 


     if (entry->d_type & DT_DIR) { 

      /* Check that the directory is not "d" or d's parent. */ 

      if (strcmp (d_name, "..") != 0 && 
       strcmp (d_name, ".") != 0) { 
       int path_length; 
       char path[PATH_MAX]; 

       path_length = snprintf (path, PATH_MAX, 
             "%s/%s", dir_name, d_name); 
       printf ("%s\n", path); 
       if (path_length >= PATH_MAX) { 
        fprintf (stderr, "Path length has got too long.\n"); 
        exit (EXIT_FAILURE); 
       } 
       /* Recursively call "list_dir" with the new path. */ 
       list_dir (path); 
      } 
    } 
    } 
    /* After going through all the entries, close the directory. */ 
    if (closedir (d)) { 
     fprintf (stderr, "Could not close '%s': %s\n", 
       dir_name, strerror (errno)); 
     exit (EXIT_FAILURE); 
    } 
} 

int main() 
{ 
    list_dir ("/usr/share/games"); 
    return 0; 
} 
+0

Thank you! отлично работает, но, может быть, вы знаете, почему он печатает в пути к каталогу также «..» и «.». ? –

+1

Если вы хотите не распечатывать неявные каталоги, вам нужно будет изменить '/ * Распечатать имя файла и каталога. */ printf ("% s /% s \ n", dir_name, d_name); ' Чтобы проверить (а не распечатать) эти два. Он делает это, когда проверяет выполнение рекурсивного вызова чуть ниже. Он не может рекурсивно вызывать функцию в этих двух каталогах, потому что иначе она никогда не завершится. – MuertoExcobito

0

Другой пример, используя файл дерева гуляем (ftw или nftw) Они имеют то преимущество, что собственный обратного вызова Funtion с struct stat, имя файла и тип (FTW_D и т.д.) Вызовите fnmatch, чтобы устранить нежелательные записи. Файлы, начинающиеся с "." «скрытые файлы». ls не показывает их по умолчанию. ls -a перечислит их.

#include <sys/types.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <ftw.h> 

int callback(const char *fname, 
      const struct stat *st, 
      int type, 
      struct FTW *ftw) 
{ 
    // call fnmatch() here or use type to decide about printing 
    // printf file name and type , ??? on stat error FTW_NS, default to "????" 
    printf("%s\n", fname); 
    return 0; 
} 
int main(int argc, char **argv) 
{ 
    int fd_max=8; // max file desriptors 
    int retval=nftw((argc==1)?"." :argv[1], callback, fd_max, FTW_ANYERR); 
    return retval; 
} 
Смежные вопросы