LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   read directory and subdirectories (http://www.linuxquestions.org/questions/programming-9/read-directory-and-subdirectories-109124/)

skora 10-27-2003 12:31 PM

read directory and subdirectories
 
i want to make program that is going to read all files from directory and all files and subdirectories from its subdirectories.

it reads all files from original directory but it has problem reading files from subdirectories.

anyone plz help!

while ((ent = readdir(dir)) != NULL ) {
filename = ent->d_name;

cout <<"filename "<<filename<<endl;
// print current directory
(void)printf("%s\n", cwd);

if (strcmp(ent->d_name,".") == 0 || strcmp(ent->d_name,"..") == 0) {
cout <<"Direcotry is /. or /..\n";
} else {

lstat(filename, &buf[1]);

if (S_ISDIR(buf[1].st_mode)) { // we found new sub directory
readsubdir(filename); //recursion
}
....

crabboy 10-27-2003 04:40 PM

I posted a program here that does what you are looking for and a bit more. You will have to strip out the code that you do not need. Including the pause and the signal stuff.

http://www.linuxquestions.org/questi...threadid=81305

SaTaN 10-27-2003 10:14 PM

I think the problem with your code is that when you are recursively entering a sub-directory you aren't sending
the entire path as the parameter.

when you stat and then find out that it is a directory then you
need to concatenate the directory name to the present working directory and then send it

I'll post the program in the next post

SaTaN 10-27-2003 10:22 PM

Code:

#include<dirent.h>
#include<sys/types.h>
#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include <grp.h>
#include <sys/types.h>
struct store_dir
{
        int dir_no[10];
        int total_dir;
};
int main()
{
        char BUF[100];
        printf("$DIR=");
        scanf("%s",BUF);
        dir_listing(BUF);
}
dir_listing(char *value)
{
        int num,i;
        struct dirent **name_list;
        struct stat BUFFER;
        struct store_dir *PDIR;
        char string[1000],date[50],descend[30];
        PDIR=(struct store_dir *)malloc(sizeof(struct store_dir));
        PDIR->total_dir=0;
        value[strlen(value)]='\0';
        num=scandir(value,&name_list,0,alphasort);
        printf("\n %s \n",value);
        if(num<0)
                printf("\nError while using scandir\n");
        else
        {
                for(i=0;i<num;i++)
                {
                        strcpy(string, value);
                        strcat(string, "/");
                        strcat(string, name_list[i]->d_name);
                        stat(string,&BUFFER);
                        if((strcmp(name_list[i]->d_name,".")!=0)&&(strcmp(name_list[i]->d_name,"..")!=0))
                        {
                                if(S_ISDIR(BUFFER.st_mode))
                                {
                                        PDIR->dir_no[(PDIR->total_dir)++]=i;
                                }
                                printf(" %s\n",name_list[i]->d_name);
                        }
                }
                for(i=0;i<PDIR->total_dir;i++)
                {
                                if(S_ISDIR(BUFFER.st_mode))
                                {
                                        sprintf(descend,"%s/\%s",value,name_list[PDIR->dir_no[i]]->d_name);
                                        dir_listing(descend);
                                }
                                free(name_list[i]);
                }
                free(name_list);
        }
}



All times are GMT -5. The time now is 07:29 AM.