linux 下 opendir 和 readdir 函数的应用

来源:互联网 发布:mac液体唇膏三文鱼粉 编辑:程序博客网 时间:2024/05/24 03:37
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <dirent.h>#include <string.h>void printall(const char *path, int max){   DIR *dir = opendir(path);   //产生DIR的指针   if (dir == NULL){     return ;   }   struct dirent *dirent;  //系统默认含有的结构体体   int t = 1;   while(dirent = readdir(dir)){   if (!strcmp(dirent->d_name,".")||!strcmp(dirent->d_name,"..")){  //遇到. 和 .. 是跳过      continue;   }   for (t = 1; t < max; ++t){   printf("--");   }       if(dirent->d_type == 4){   printf("[%s]\n",dirent->d_name);       char tmp[100] = {0};   sprintf(tmp,"%s/%s",path,dirent->d_name);   // 需要进行地址的拼接,否则不能正确往下执行   printall(tmp,max + 1);   }   printf("%s\n",dirent->d_name);      }}int main(void){    printall("../../",1);return 0;}



0 0
原创粉丝点击