Link to home
Start Free TrialLog in
Avatar of arijit_rebaca
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;
}
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi arijit_rebaca,

Perhaps it should be:

          printf("%s\n", drp->d_name);

:-)


Paul
Avatar of manish_regmi
manish_regmi

also, do NOT use #include <bits/dirent.h> directly.
It is unportable. dirent.h is enough

regards
Manish Regmi

Avatar of arijit_rebaca

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

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
Avatar of kaliyugkaarjun
kaliyugkaarjun

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi  kaliyugkaarjun,

The o/p come always same :

i.e.  ..\     ..\           ..\ infinite times



Hi kaliyugkaarjun ,

There will be another entry

if (strcmp(".", entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)



any way.......
thanks a lot