-
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.
-
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.