Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to skip subdirectories while reading a directory 1

Status
Not open for further replies.

naru21

Technical User
May 24, 2002
13
US
Hi All,
I am trying to read all the files in a directory if there are any subdirectories i want to skip them. I wrote a small script but this reads sub directories also. Is there a way to skip subdirectories?

#include <stdio.h>
#include <dirent.h>

main(int argc, char **argv) {
DIR *D1;
struct dirent *dp;
short ret=0;

printf("input args %s",argv[1]);
while(!ret) {
if((D1=opendir(argv[1]))==NULL) {
printf("Failed to open files \n");
ret = 1;
} else {
while( (dp = readdir(D1)) != NULL) {
if (!strcmp(dp->d_name, ".")) continue;
printf("After first if condition \n");
if (!strcmp(dp->d_name, "..")) continue;
printf("After second if condition \n");
printf("FileName is [%s] \n",dp->d_name);

} /* while dp loop */
ret=1;
} /* else loop */
} /* while loop */
closedir(D1);
}

Thanks for your help!
 
man 2 stat?

Code:
int checkType(char *fname) {
struct stat foo;

           if (fname == NULL) {return -1;}
           if (stat(fname,&foo) != 0) {
               perror("stat()");
               return -1;
           }     
        
           if (S_ISREG(foo.st_mode)) {return 1;}
           if (S_ISDIR(foo.st_mode)) {return 0;}
}

HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top