递归输出指定目录下的所有文件名

http://topic.csdn.net/u/20080106/19/6f6ce379-d536-4699-a475-6e834a24d74b.html
#include   <unistd.h>
#include   <stdio.h>
#include   <dirent.h>
#include   <string.h>
#include   <sys/stat.h>

int printpath(char *  path )
{
   DIR *directory;
   struct      dirent * dir_entry;
   char        buffer[128];

   struct  stat  buff;
   if ((directory=opendir(path)) == NULL) { 
       fprintf(stderr, "%s", path);
       perror(" ");
       return -1;
   }

     while (dir_entry=readdir(directory)) {
     if  (!strcmp(dir_entry->d_name,".")||!strcmp(dir_entry->d_name,"..")) {
              /*  do    nothing*/
     }
     else    
     {
             sprintf(buffer,"%s/%s",path,dir_entry->d_name);

           if (lstat(buffer,&buff)<0)
             continue;

           if(S_ISDIR(buff.st_mode))
           {       
             printf(" \n directory -%s \n \n", buffer);
             printpath(buffer);
           }       
           else    
           {       
             printf("file name = %s path= -%s \n" ,dir_entry->d_name, path);
           }       
      }
    }

    closedir(directory);

}

int main()
{
        printpath("/root/util/tmp");
        return 0;
}

你可能感兴趣的:(struct,File,null,buffer,Path)