LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-27-2003, 12:31 PM   #1
skora
LQ Newbie
 
Registered: Aug 2003
Location: Somewhere out in space
Distribution: SuSe 8.0
Posts: 18

Rep: Reputation: 0
Unhappy 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
}
....
 
Old 10-27-2003, 04:40 PM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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
 
Old 10-27-2003, 10:14 PM   #3
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
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
 
Old 10-27-2003, 10:22 PM   #4
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
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);
        }
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy 3 files to a directory and all subdirectories? snip128 Linux - Newbie 9 08-12-2005 09:18 PM
why can't the /bin directory contain subdirectories? learnfast Linux - Newbie 2 03-09-2005 11:31 AM
Copying all files from subdirectories into one directory Hegemon Linux - General 3 01-17-2005 11:25 AM
howto mv a set of subdirectories to an other directory? Lleb_KCir Linux - General 1 06-13-2004 11:33 PM
How to know the property a directory include the files and subdirectories? Xiangbuilder Linux - Newbie 3 08-31-2003 05:48 AM


All times are GMT -5. The time now is 09:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration