Thread: Recursively list files inside dir

  1. #1
    Registered User
    Join Date
    Nov 2010

    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?

     for now my function only hashes the file in the top level root_dir
    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) {
    				continue; /* for now I'm just skipping dirs... */
    			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. */
    		closedir (dir);
    	} else {
    		/* could not open directory */
    		perror ("opendir()");

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

  2. #2
    Registered User
    Join Date
    Sep 2007
    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
    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

    [ ]'s
    Last edited by b1nd3r; 11-23-2010 at 04: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, 01: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
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

Tags for this Thread