Recursively list files inside dir

This is a discussion on Recursively list files inside dir within the C Programming forums, part of the General Programming Boards category; Hey folks I'm stuck with a recursion problem here, I need to create a hash for each file inside some ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    7

    Question Recursively list files inside dir

    Hey folks I'm stuck with a recursion problem here, I need to create a hash for each file inside some directory, the problem is when it comes to other directories inside, I need the files inside them and inside their dirs too, and so on recursively. Can someone give me a hand on this peace of code?

    Code:
    /*
     for now my function only hashes the file in the top level root_dir
    */
    int
    gen_files_hashes(char *root_dir) {
    	
    	DIR *dir;	
    	char b[512];
    	int i=0,k;
    	struct dirent *ent;
    	
    	dir = opendir (root_dir);
    	if (dir != NULL) {
    		while ((ent = readdir (dir)) != NULL) {
    
    			if(ent->d_type==DT_DIR)
    				continue; /* for now I'm just skipping dirs... */
    				
    			b[0]='\0';
    			strncat(b, root_dir, sizeof(b)-1);
    			strncat(b, "/", sizeof(b)-1);
    			strncat(b, ent->d_name, sizeof(b)-1);
    			
    			gen_file_hash(b); /* Here is where I print the file hash. */
    			i++;
    		}
    		closedir (dir);
    	} else {
    		/* could not open directory */
    		perror ("opendir()");
    	}
    }

    Thanks
    [ ]'s
    Last edited by b1nd3r; 11-22-2010 at 09:27 PM. Reason: typo

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    First, ignore the entries "." and "..". Next, if it's a directory, call gen_files_hashes(b). That's all.

    Note that d_type is not standard, and where it does exist, not all filesystems necessarily support it. To be portable, you'll need to use stat().

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    Hi cas,

    well, this application is supposed to be used only on Linux but is good to know that isn't an standard I have a function using stat made just to see if the path is a directory, maybe after resolving this problem I start using it.

    and about your solution I think I see what you mean, and now looks so obvious, let's see if works, thanks!

    edit: I changed my code and now it's perfect, thanks by the hint

    regards
    [ ]'s
    Last edited by b1nd3r; 11-23-2010 at 03:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Circularly-Doubly Linked List implementation
    By BlackOps in forum C Programming
    Replies: 4
    Last Post: 07-19-2009, 04:45 AM
  2. circularly linked list
    By BlackOps in forum C Programming
    Replies: 0
    Last Post: 07-18-2009, 08:12 AM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

Tags for this Thread


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