Directory Tree Display Problem

This is a discussion on Directory Tree Display Problem within the Linux Programming forums, part of the Platform Specific Boards category; Code: void dirBrowser(char *curDir, int tab, int nA, int nI, int nS, int nT) { DIR *dir; FILE *pFile; struct ...

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    Directory Tree Display Problem

    Code:
    void dirBrowser(char *curDir, int tab, int nA, int nI, int nS, int nT)
    {
    
            DIR *dir;
            FILE *pFile;
            struct dirent *de;
            struct stat st;
            char *symPath, symBuf[1024];
            ssize_t len;
            int x=0, size=0;
    
            chdir (curDir);
    
            if (nT==1 && nI==0)
                    for (x=0; x<tab; x++)
                            printf("\t");
            else if (nI==1)
                    for (x=0; x<nNopt; x++)
                            printf(" ");
            dir = opendir(".");
            while (de = readdir(dir))
             {
                    stat(de->d_name, &st);
                    if ((len = readlink(de->d_name, symBuf, sizeof(symBuf)-1))!=-1)
                    {
                            chdir (symBuf);
                    }
                    size=0;
     size += st.st_size;
                    size = size/1024;                                               // Block Size
                    if (nA==1 && nS==0)
                            printf ("%d %s\n",size,  de->d_name);                   // Print Everything
                    if (nS==1 && nA==0)
                            printf ("%d\n", size);
    
                    if ((S_ISDIR(st.st_mode) && nA == 0))
                     {
                            if ((strcmp(de->d_name, ".") != 0) && (strcmp(de->d_name, "..") != 0))
                            {
                    if (nS==1 && nA==0)
                            printf ("%d\n", size);
    
                    if ((S_ISDIR(st.st_mode) && nA == 0))
                     {
                            if ((strcmp(de->d_name, ".") != 0) && (strcmp(de->d_name, "..") != 0))
                            {
                                    if (nS==0 && nA==0)
                                            printf ("%d %s\n",size, de->d_name);// Print Directories only
                                    dirBrowser(de->d_name, tab+1, nA, nI, nS, nT);
                            }
                    }
            }
            closedir(dir);
            chdir("..");
    }
    Directory Content (du)
    Code:
    4       ./dir1/dir11/dir111/dir1111
    8       ./dir1/dir11/dir111
    12      ./dir1/dir11
    24      ./dir1
    4       ./dir2
    4       ./dir3/dir31
    4       ./dir3/dir32/dir321
    8       ./dir3/dir32
    4       ./dir3/dir33
    20      ./dir3
    4       ./dir4
    4       ./dir22
    104     .
    Directory Content (my app)
    Code:
    4 dir1
    4 dir11
    4 dir111
    4 dir1111
    4 dir2
    4 dir3
    4 dir31
    4 dir32
    4 dir321
    4 dir33
    4 dir4
    4 dir22
    With Tree option
    Code:
    4 dir1
            4 dir11
                    4 dir111
                            4 dir1111
                                    4 dir2
            4 dir3
            4 dir31
                    4 dir32
                    4 dir321
                            4 dir33
                    4 dir4
            4 dir22
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Is this a question?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    yes

    my tree is not indenting properly.
    Dir1, Dir2, Dir3, and Dir4 are parent Directories located in "." directory.
    If you look above at Tree Display: Dir2,3,4 are indented wrong.
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Perhaps you need your tabbing code inside the loop, like before each printf
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    C:\Documents and Settings\????>tree
    Folder PATH listing
    Volume serial number is ??????/?????/????/
    C:.
    ├───.java
    ├───.jpi_cache
    │   ├───file
    │   │   └───1.0
    │   └───jar
    │       └───1.0
    ├───Desktop
    │   ├───My Music
    │   │   └───New Folder
    │   ├───stuff
    │   └───The room
    │       ├───box 1
    │       │   ├───box 1.1
    │       │   └───box 1.2
    │       │       └───box 1.2.1
    │       └───box 2
    ├───Favorites
    │   └───Links
    ├───My Documents
    │   ├───My eBooks
    │   ├───My Music
    │   ├───My Pictures
    │   └───PDF files
    │       └───AutoSave
    └───Start Menu
        └───Programs
            ├───Accessories
            │   ├───Accessibility
            │   └───Entertainment
            ├───Java Web Start
            ├───pdfFactory Pro
            └───Startup
    I got this from the command prompt using Windows. Is this something what you wanted? If so recursion may be your answer perhaps?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree Search
    By C++Newbie in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2011, 01:17 AM
  2. Please help! Dynamic binary tree problem
    By robsmith in forum C Programming
    Replies: 2
    Last Post: 03-15-2005, 08:56 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. display tree data in binary tree format
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 11:47 PM
  5. Problem with binary search tree :(
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 05-01-2002, 10:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21