arijit_rebaca
asked on
directory list
HI,
here is my program...... It gives an error: has no menber named 'd_name' ...... where is the problem... pls help me..
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <bits/dirent>
int main()
{
DIR *dp;
struct dirent *drp;
/* open the current working driectory */
if((dp = opendir(".")) == NULL) {
perror("opendir");
exit(1);
}
while((drp = readdir(dp)) != NULL)
{
printf("%s\n", drp->d_nane);
}
closedir(dp);
return 0;
}
here is my program...... It gives an error: has no menber named 'd_name' ...... where is the problem... pls help me..
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <bits/dirent>
int main()
{
DIR *dp;
struct dirent *drp;
/* open the current working driectory */
if((dp = opendir(".")) == NULL) {
perror("opendir");
exit(1);
}
while((drp = readdir(dp)) != NULL)
{
printf("%s\n", drp->d_nane);
}
closedir(dp);
return 0;
}
also, do NOT use #include <bits/dirent.h> directly.
It is unportable. dirent.h is enough
regards
Manish Regmi
It is unportable. dirent.h is enough
regards
Manish Regmi
ASKER
The problem I have faced is solved....
Now the another problem is I want to list down all the directory and subdirectory under a root directory, where my app file is present. How I do that..
Test
\
HP RP
\ \
X y Z a b c
my file is in the same directory where Test is located.... Now I want to list down all directory and sub-directory under Test..
How can I do it..... pls tell me as soon as posssible
Now the another problem is I want to list down all the directory and subdirectory under a root directory, where my app file is present. How I do that..
Test
\
HP RP
\ \
X y Z a b c
my file is in the same directory where Test is located.... Now I want to list down all directory and sub-directory under Test..
How can I do it..... pls tell me as soon as posssible
Little modification ...just try this.
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
int main()
{
DIR *dp;
struct dirent *drp;
struct stat buf;
/* open the current working driectory */
if((dp = opendir(".")) == NULL) {
perror("opendir");
exit(1);
}
while (drp = readdir(dp)) {
if (stat(drp->d_name,&buf) == 0)
if(buf.st_mode & S_IFDIR)
printf("%s\n", drp->d_name);
}
return closedir(dirp);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi kaliyugkaarjun,
The o/p come always same :
i.e. ..\ ..\ ..\ infinite times
The o/p come always same :
i.e. ..\ ..\ ..\ infinite times
ASKER
Hi kaliyugkaarjun ,
There will be another entry
if (strcmp(".", entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)
any way.......
thanks a lot
There will be another entry
if (strcmp(".", entry->d_name) == 0 || strcmp("..",entry->d_name)
any way.......
thanks a lot
Perhaps it should be:
printf("%s\n", drp->d_name);
:-)
Paul