Solved

directory list

Posted on 2006-07-17
7
242 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
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
Integrate social media with email signatures

Is your company active on social media? Do you also use email signatures? Including social media icons in your email signature is a great way to get fans for free. Let all your email users know you’re on social media quickly and easily, in a single click.

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

947 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now