C program that accepts a valid directory names as a command line argument and lists all the files in the given directory as well as all the subsequent sub directories

#include
#include
#include
int myfun(const char *pathname,const struct stat *statptr,int type);
int main(int argc,char *argv[])
{
nftw(argv[1],myfun,0,0);
exit(0);
}
int myfun(const char *pathname,const struct stat *statptr,int type)
{
if(type==FTW_F)
printf("\nfile is:%s",pathname);
if(type==FTW_D)
printf("directory:%s\n",pathname);
return 0;
}
*******************************
OUTPUT:
.a/.out dir3
directory:dir3
directory:dir3/x1
file is:dir3/x1/m1directory:dir3/x2
********************************

Leave a Reply