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
Solved

directory list

Posted on 2006-07-17
7
244 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 how to use strings and some functions related to them 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.

856 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