Thread: Pass

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    25

    Pass

    I have the following code:

    Code:
    void printdir( )
    {
        DIR *dp;
        struct dirent *entry;
        struct stat statbuf;
        int depth = 0;
        char *dir = ".";
    
        if((dp = opendir(dir)) == NULL) {
            fprintf(stderr,"cannot open directory: %s\n", dir);
            return;
        }
        chdir(dir);
        while((entry = readdir(dp)) != NULL) {
            lstat(entry->d_name,&statbuf);
            if(S_ISDIR(statbuf.st_mode)) {
                /* Found a directory, but ignore . and .. */
                if(strcmp(".",entry->d_name) == 0 || 
                    strcmp("..",entry->d_name) == 0)
                    continue;
                printf("%*s%s/\n",depth,"",entry->d_name);
    		
                /* Recurse at a new indent level */
                printdir(entry->d_name,depth+4);
            }
            else printf("%*s%s\n",depth,"",entry->d_name);
        }
        chdir("..");
        closedir(dp);
    }
    I want to call a funtion that performs stuff against each file, how would I pass the file name stored in entry->d_name?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What do you mean? You're already doing it with lstat(), strcmp(), and printf()...
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    25
    I want to be able to tell another code chunk what the file is and too perform stuff on it

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    25
    would fopen (entry->d_name, "rb") work?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It will, if you have it set up correctly. Right now you're passing arguments to printdir(), but printdir() isn't accepting them. You're going to have to rethink how you have the function parameters set up. If you want to pass a dir name and a depth to it then those variables will have to be in the parameter list. This can be easily done if the original call to the function is done something like: printdir(".", 0);
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    25
    Like this, the code still does not access directories inside the other

    Code:
    #include <unistd.h>
    #include <stdio.h>
    #include <dirent.h>
    #include <string.h>
    #include <sys/stat.h>
    
    void printdir(char *dir, int depth)
    
    {
    
        DIR *dp;
    
        struct dirent *entry;
    
        struct stat statbuf;
    
        if((dp = opendir(dir)) == NULL) {
    
            fprintf(stderr,"cannot open directory: %s\n", dir);
    
            return;
    
        }
    
        chdir(dir);
    
        while((entry = readdir(dp)) != NULL) {
    
    
    
            if(S_ISDIR(statbuf.st_mode)) {
    
                /* Found a directory, but ignore . and .. */
    
                if(strcmp(".",entry->d_name) == 0 ||
    
                    strcmp("..",entry->d_name) == 0)
    
                    continue;
    
                printf("%*s%s/\n",depth,"",entry->d_name);
    
                /* Recurse at a new indent level */
    
                printdir(entry->d_name,depth+4);
    
            }
    
            else printf("%*s%s\n",depth,"",entry->d_name);
    
        }
    
        chdir("..");
        closedir(dp);
    
    }
    
    /*  Now we move onto the main function.  */
    
    int main(int argc, char* argv[])
    
    {
    
        char *topdir, pwd[2]=".";
    
        if (argc != 2)
    
            topdir=pwd;
    
        else
    
            topdir=argv[1];
    
        printf("Directory scan of %s\n",topdir);
    
        printdir(topdir,0);
    
        printf("done.\n");
    
        exit(0);
    
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The problem is you call printdir() recursively without any regard to the parameters you're passing.
    Use stat() to determine whether the file is a file (just do stuff with it) or a directory (call yourself recursively).

    There's examples of this in the FAQ.
    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.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    25
    Thank you for all your help

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    Your real problem is that you are trying to use data in statbuf (which you have declared)
    to select out directories without ever filling the buffer with any data (use fstat)!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  2. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  3. Pass by reference
    By mcgeady in forum C Programming
    Replies: 11
    Last Post: 02-17-2005, 03:01 AM
  4. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:03 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM