Thread: Directory listing

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    23

    Directory listing

    Hi,

    I wrote this recursive function to 'pretty print' directory listings but for some reason it only recurses down through one directory?

    Code:
     
    void recursive_dir(DIR* directory,int tabs){ 
       
      if(!(directory)) { 
        error("Directory is not accesible"); 
        return; 
      } 
       
      struct dirent *dip; 
     
      while(( dip = readdir( directory ))){ 
        if(!strcmp(dip->d_name, ".") || !strcmp(dip->d_name, "..")) 
          continue; 
         
        DIR* d = opendir(dip->d_name); 
        if(d != NULL){ 
          cout << "|-**"; 
          for(int i=0;i<tabs;i++)  // Print out the tabs 
    	cout << "---"; 
          cout << dip->d_name << "\n"; 
          recursive_dir(d,tabs+1); 
          closedir( d ); 
          continue; 
        } 
        
        for(int i=0;i<tabs;i++)  // Print out the tabs 
          cout << "   "; 
         
        cout << "|- " << dip->d_name << "\n";   
      } 
       
      return; 
    }
    --------------------------------------------------------------------------------


    Think I may have used opendir() wrong but cant see where,

    thanks for any help

    Brif

    ps I also posted this on the c board but was asked to put it here, sorry about that.

  2. #2
    Registered User shuesty's Avatar
    Join Date
    Oct 2002
    Posts
    21
    Without actually loading up your code it's hard to understand where the problem is because the code looks fine. I know there is a function in C that can check if your variable 'd' is a directory or not which you mihgt want to look into. The next thing you might want to do is start putting in some printf's to find out what everything is holding ... dip->d_name, directory's name and d's name. When you find out what the computer is looking at you'll be able to figure out your problem ... and it you already knew that then disregard this whole message.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  2. Listing specific files in a directory
    By knirirr in forum C Programming
    Replies: 14
    Last Post: 01-29-2008, 05:42 AM
  3. Replies: 4
    Last Post: 06-05-2006, 03:24 AM
  4. recursive directory listing help
    By laney69er in forum C Programming
    Replies: 2
    Last Post: 03-13-2006, 01:07 PM
  5. Directory Listing
    By Angelus in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2002, 09:32 AM