Solved

directory list

Posted on 2006-07-17
7
241 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

743 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

11 Experts available now in Live!

Get 1:1 Help Now