English 中文(简体)
利用C++印刷所有目录
原标题:Printing all the directories using c++
  • 时间:2011-10-02 20:50:58
  •  标签:
  • c++

该方案正在印刷原层目录。

Directory_1
Directory_2

但是,我也希望能够在其内部印刷目录。

Directory_1
   Directory_1_2
   Directory_1_3
Directory_2
   Directory 2_1
      Directory_2_1_1
Directory_4

我试图 do,但我发现,很难把《名录》1作为根基,从而得到评价。 什么是失踪?

我的产出

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

法典

#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
", 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
", direntp->d_name);
   arg_tmp = direntp->d_name;
  }    
   printDepthFirst(arg_tmp);

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

}

现在,我知道有些人在问问他们是否认为我期望他们把这个问题编成法典时,会感到不快,如果你能够从理论上告诉我需要做些什么的话,你就迫切需要这样做。 我将研究这个问题,尽管它是一个小的方案解决方案,而且你可以说,我真的会理解它。 我也想听说需要做些什么。

谢谢。

最佳回答

这样做有助于:

#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
", 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;
}
问题回答

查阅<代码> 结构可怕的。

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:AlphaBeta", d_name would only contatin "Beta", not "C:AlphaBeta". 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
", current);
   printDepthFirst (current);
}

此外,你还应在休息室内,而不是在外面重新打电话。

在您的<代码>上,, loop in DepthFirst, 您可能需要:

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

也许你不得不担心<代码>。

或者,我发现有增益:档案系统运转良好。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签