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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using ftw()

Status
Not open for further replies.

CaptRage61

Programmer
Feb 17, 2005
50
US
Does anyone know how to use file tree walk, ftw() to list all of the files and directories and the directories files? I want to create a quick little program like the other one I was trying to write that displays everything recursivly with ftw().
 
What OS, what architecture, what code do you already have, etc..
 
OS is linux

Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>


void search ( char *filename , char *dirname ){
  int f = 0;
  struct dirent *dirPointer;
  DIR *d;
  d = opendir( dirname );


   if ( d == NULL ) {
        fprintf( stderr, "%s %d: opendir() failed (%s)\n",
            dirname, errno, strerror( errno ));
        exit( EXIT_FAILURE );
    }
   while ( (dirPointer=readdir(d)) != NULL ) {
     if (strcmp( dirPointer->d_name, filename) == 0 ) {
         // print absolute path of file
         printf("File located\n");
         f = 1;
       }

}
if ( f == 0 ) {
printf("File not found\n");
}
closedir ( d );
printf("\n");
}

int main( int argc, char *argv[] ) {
    int x = argc;
    int count;
    printf("Number of args: %d\n",x);
     if ( x == 2 ){
      //search for file or dir in current dir
      printf("Searching\n");
      search( argv[1] , "." );
    }
     if ( x > 2 )  {
        //first n-1 are files to search for
        //n is the folder to search

        for ( count = 2 ; count < argc ; count++ ) {
            search( argv[count],argv[argc]);

}
    }
     if ( x == 1 ) {
        printf("Not enough input\n");
     }

return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top