|
|
| coolbuddyguy@gmail.com 2006-04-11, 9:59 am |
| i have found sum problem in running this program...coz it is not
running as it shud run for ls -a ...i am unable to depict what is the
prob in the prog. can anyone plz tell me what could be the changes and
where i am wrong...thanks in advance
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
void dirr(DIR *dp)
{
struct dirent *dirp;
DIR *dp1;
char *p;
while ( (dirp = readdir(dp)) != NULL)
{
printf("%s\n",dirp->d_name);
dp1=opendir(dirp->d_name);
if(dp1!=NULL && dirp->d_name!="." && dirp->d_name!="..")
{
printf("\n\n\n");
dirr(dp1);
}
}
}
int main(int argc, char *argv[])
{
DIR *dp,*dp1;
struct dirent *dirp;
if (argc != 2)
{
perror("a single argument (the directory name) is required");
exit(1);
}
if ( (dp = opendir(argv[1])) == NULL)
{
perror("can't open");
exit(1);
}
dirr(dp);
closedir(dp);
exit(0);
}
| |
|
| coolbuddyguy@gmail.com wrote:
> if(dp1!=NULL && dirp->d_name!="." && dirp->d_name!="..")
^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
strcmp() is your friend.
| |
| coolbuddyguy@gmail.com 2006-04-11, 9:59 am |
| if(dp1!=NULL && strcmp(dirp->d_name,".") && strcmp(dirp->d_name,".."))
this is the change i made but it does not solve my problem....bcoz now
it is not showing a full output i.e. not showing all the files in the
sub directories...can u help a bit more
| |
| Ralf Fassel 2006-04-11, 9:59 am |
| * "coolbuddyguy@gmail.com" <coolbuddyguy@gmail.com>
| this is the change i made but it does not solve my problem....bcoz
| now it is not showing a full output i.e. not showing all the files
| in the sub directories...can u help a bit more
The opendir() call in dirr() tries to open the directory name relative
to the current working directory. Use a debugger or print statements
to see what you pass to opendir.
R'
| |
| coolbuddyguy@gmail.com 2006-04-27, 7:55 am |
| i have alreay given a printf() statement before the opendir() which
prints the argument tht is going to be passed in opendir() ..........
| |
| Ralf Fassel 2006-04-27, 7:55 am |
| * "coolbuddyguy@gmail.com" <coolbuddyguy@gmail.com>
| i have alreay given a printf() statement before the opendir() which
| prints the argument tht is going to be passed in opendir()
And what did you see?
Hint: if you have the following directory structure in ./:
./dir1/
./dir1/dir2/
then your program tries to open in order:
dir1
dir2
Do you see why opening plain dir2 won't work while you're in ./ ?
R'
| |
| coolbuddyguy@gmail.com 2006-04-27, 7:55 am |
| thanx for reply ...yes you are right...it opens in the same order but
what i found that it does not print all the subdirectories and files
within the directory....as it shud go for the recursion and shud print
all the subdirectories and files...but it is not doing so...plz help
| |
| Ralf Fassel 2006-04-27, 7:55 am |
| * "coolbuddyguy@gmail.com" <coolbuddyguy@gmail.com>
| within the directory....as it shud go for the recursion and shud print
| all the subdirectories and files...but it is not doing so...plz help
Say you want to open a file 'subdir/filename' in a subdirectory.
Which file name do you have to pass to open? Try both:
open("filename");
open("subdir/filename");
The same goes for opening directories in subdirectories.
R'
| |
| coolbuddyguy@gmail.com 2006-04-27, 7:55 am |
| thanks i got ur idea...there shud be liitle change when i pass the
directory/file name in opendir()....thanx once again
|
|
|
|