简体   繁体   中英

Printing all the directories using c++

This program is printing the directories at the root level

Directory_1
Directory_2

but I want to be able to print the directories within them too

Directory_1
   Directory_1_2
   Directory_1_3
Directory_2
   Directory 2_1
      Directory_2_1_1
Directory_4

I am trying to do it recursively but I am finding it hard to pass the Directory_1 as a root so it gets evaluated.. What am i missing?

Here is my output

..
.
Directory_1
Directory_2
Failed to open directory: No such file or directory

Code

#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>

char *arg_temp;

int printDepthFirst(char *arg_tmp);

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

   if (argc != 2) {
      fprintf(stderr, "Usage: %s directory_name\n", argv[0]);
      return 1; 
   }  


  arg_temp = argv[1]; 

  printDepthFirst(arg_temp);

}

int printDepthFirst(char *arg_tmp)
{

   struct dirent *direntp;
   DIR *dirp;

   if ((dirp = opendir(arg_tmp)) == NULL) {
      perror ("Failed to open directory");
      return 1;
   }  


   while ((direntp = readdir(dirp)) != NULL)
  {
   printf("%s\n", direntp->d_name);
   arg_tmp = direntp->d_name;
  }    
   printDepthFirst(arg_tmp);

  while ((closedir(dirp) == -1) && (errno == EINTR)) ;
     return 0;

}

Now, I know some people get irritated when asking questions that they think I am expecting them to code this, you dont need to, if you can tell me theoretically what i need to do.. I will research it although if its a small programmatically fix and you can post that I would really appreaciate it.. but if not.. I would also love to hear about what needs to be done in words..

Thank you

Well this should help:

#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <stdio.h>

static int display_info(const char *fpath, const struct stat *sb,
             int tflag, struct FTW *ftwbuf)
{
    switch(tflag)
    {
        case FTW_D:
        case FTW_DP: puts(fpath); break;
    }
    return 0; /* To tell nftw() to continue */
}

int main(int argc, char *argv[])
{
    if (argc != 2) {
        fprintf(stderr, "Usage: %s directory_name\n", argv[0]);
        return 1; 
    }  

    int flags = FTW_DEPTH | FTW_MOUNT | FTW_PHYS;

    if (nftw(argv[1], display_info, 20, flags) == -1)
    {
        perror("nftw");
        return 255;
    }

    return 0;
}

看一下struct dirent包含哪些字段。

The string dirent::d_name is a name of a directory, not it's full path. So, if your directory "C:\\Alpha" contains directory "C:\\Alpha\\Beta", d_name would only contatin "Beta", not "C:\\Alpha\\Beta". You will have to assemble the full path yourself - appending slash/backslash to your arg_tmp and then appending new directory name, like this:

while ((direntp = readdir (dirp)) != NULL)
{
   char *dirname = direntp->d_name;

   // Only work with directories and avoid recursion on "." and "..":
   if (direntp->d_type != DT_DIR || !strcmp (dirname, ".") || !strcmp (dirname, "..")) continue;

   // Assemble full directory path:
   char current [strlen (arg_tmp) + 2 + strlen (dirname)];
   strcpy (current, arg_tmp);
   strcat (current, "\\"); // Replace "\\" with "/" on *nix systems
   strcat (current, dirname);

   // Show it and continue:
   printf ("%s\n", current);
   printDepthFirst (current);
}

Also, you should call recursively inside the loop, not outside.

Inside your while loop inside printDepthFirst you might need something like:

if(direntp->d_type == DT_DIR)
    printDepthFirst(directp->d_name);

You might perhaps have to worry about .. directories too.

Alternatively, I've found boost::filesystem to work quite well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM