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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-27-2003, 12:31 PM
|
#1
|
LQ Newbie
Registered: Aug 2003
Location: Somewhere out in space
Distribution: SuSe 8.0
Posts: 18
Rep:
|
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
}
....
|
|
|
10-27-2003, 04:40 PM
|
#2
|
Senior Member
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821
Rep: 
|
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
|
|
|
10-27-2003, 10:14 PM
|
#3
|
Member
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223
Rep:
|
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
|
|
|
10-27-2003, 10:22 PM
|
#4
|
Member
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223
Rep:
|
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 03:00 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|