Solved

directory list

Posted on 2006-07-17
7
246 Views
Last Modified: 2011-09-20
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;
}
0
Comment
Question by:arijit_rebaca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17126585
Hi arijit_rebaca,

Perhaps it should be:

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

:-)


Paul
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17127539
also, do NOT use #include <bits/dirent.h> directly.
It is unportable. dirent.h is enough

regards
Manish Regmi

0
 

Author Comment

by:arijit_rebaca
ID: 17128125
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17128130

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);
}

0
 
LVL 8

Accepted Solution

by:
kaliyugkaarjun earned 400 total points
ID: 17128193

/*
* This program should list the directory's + subdirs
* The search should begin on the location specief in
* main()
*
* This is a program taken from "Beginning Linux Programming"
*/

#include <unistd.h>
#include <stdio.h>
#include <dirent.h> /* All header files */
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>

/* Here we write the printdir function */
void printdir(char *dir, int depth)
{

DIR *dp;
struct dirent *entry;
struct stat statbuf;

/* We use opendir() to check wether the
directory from main() exists */
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}

/* If the directory exists we use chdir() to go into the
directory.
If the directory does not exist we printf above error

We use a recursive while loop to make sure it continues
aslong as
there are unlisted directory's */

chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry -> d_name, &statbuf);
if(S_ISDIR(statbuf.st_mode)) {
if (strcmp(".", entry -> d_name) == 0)
continue;
printf("%*s%s/\n", depth,"", entry -> d_name);
printdir(entry -> d_name, depth + 4);
}
else printf("%*s%s/\n", depth,"", entry -> d_name);
}

/* When the program is doen crawling through the directory's
it use chdir()
to go back to the parent directory where it calls
closedir() to close the
opendir stram */

chdir("..");
closedir(dp);
}

int main()
{

printf("Directory scan of /home/roeland/programming:\n");
printdir("/home/roeland/programming",0); /* The
directory that needs to be listed */
printf("Done!\n");

return 0;
}
0
 

Author Comment

by:arijit_rebaca
ID: 17128439
Hi  kaliyugkaarjun,

The o/p come always same :

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



0
 

Author Comment

by:arijit_rebaca
ID: 17128528
Hi kaliyugkaarjun ,

There will be another entry

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



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


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question