Pass

This is a discussion on Pass within the C Programming forums, part of the General Programming Boards category; I have the following code: Code: void printdir( ) { DIR *dp; struct dirent *entry; struct stat statbuf; int depth ...

  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,163
    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,163
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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-21-2007, 11:58 PM
  3. Pass by reference
    By mcgeady in forum C Programming
    Replies: 11
    Last Post: 02-17-2005, 02: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, 12:39 PM

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