天天看點

linux 平台遞歸讀取讀取所有檔案夾和檔案名稱

 #include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<sys/types.h>

#include<dirent.h>

#define MAX 1024

char path[100];

char *strmin(char *des,const char *min)

{

 int i=strlen(des);

 int j=strlen(min);

 if(i>=j)

 {

  strncpy(des+i-j,"/0",1);

 }

 return des;

}

void print(char *path,char *tmpname)

{

 struct stat buf;

 if(stat(path,&buf)<0)

 {

  printf("stat error!/n");

  return;

 }

 struct dirent *filename;

 if(S_ISDIR(buf.st_mode))

 {

  DIR *dp;

  dp=opendir(path);

  while(1)

  {

   filename=readdir(dp);

   if(filename == NULL)

   {

    strmin(path,tmpname);

    closedir(dp);

    return ;

   }

   else

   {

    if(strcmp(filename->d_name,".")==0 ||strcmp(filename->d_name,"..")==0)

    continue;

    printf("%s/n",filename->d_name);

    strcat(path,"/");

    strcat(path,filename->d_name);

    print(path,filename->d_name);

    strmin(path,"/");

   }

  }

 }

 else

 {

  int fd;

  fd=open(path,O_RDWR);

  char buf[MAX];

  int size;

  while(1)

  {

   size=read(fd,buf,MAX); 

   if(size==0)

   {

    close(fd);

    break;

   }

  }

  strmin(path,tmpname);

 }

}

int main(int argc,char**argv)

{

 strcpy(path,argv[1]);

 print(path,"/0");

}

繼續閱讀