8.目录相关函数:chdir/getcwd/mkdir;[opendir;readdir;closedir]

在这里插入图片描述

示例代码

  chdir("/home/gjw");  
                                                         
  char buf[1024];                                                      
  getcwd(buf,sizeof(buf));                                             
  printf("pwd=%s\n",buf);                                              

  mkdir("gjw_dir",777);   

在这里插入图片描述

1.读取一个目录下的[第一层]的所有内容

opendir进入目录之后
需要循环去读取目录readdir
while(readdir()!=NULL){
}
最后关闭目录closedir

2.作业:实现tree [Dir]的功能
递归读取目录里面的文件数:https://blog.csdn.net/lzjsqn/article/details/53511626

 #include <stdio.h>                                                                                                             
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>                                                                                                            

 #include <sys/stat.h>
 #include <sys/types.h>
 #include <fcntl.h>                                                                                                             
 
 #include <dirent.h>

 int travel_dir(char* dir){                                                                                                     
  DIR* ptr=opendir(dir);                                                                                                        
  if(ptr==NULL){                                                                                                                
    perror("opendir fail");                                                                                                     
    exit(1);                                                                                                                    
   }                                                                                                                            
  struct dirent* drt=(struct dirent*)malloc(sizeof(struct dirent));                                                             
  while((drt=readdir(ptr))!=NULL){                                                                                              
    if(strcmp(".",drt->d_name) == 0 || strcmp("..",drt->d_name) == 0){                                                          
      continue;                                                                                                                 
    }                                                                                                                           
    char buf[1024];                                                                                                             
    memset(buf,0,sizeof(buf));                                                                                                  
    sprintf(buf,"%s/%s",dir,drt->d_name);                                                                                       
                                                                                                                                
    if(drt->d_type==DT_DIR){                                                                                                    
      printf("%s\n",buf);                                                                                                       
      travel_dir(buf);                                                                                                          
    }                                                                                                                           
    if(drt->d_type==DT_REG)                                                                                                     
      printf("%s\n",buf);                                                                                                       
  }                                                                                                                             
  closedir(ptr);                                                                                                                
}          

int main(int args,char* argv[]){                                                                                               
  if(args<2){                                                                                                                  
    perror("args<2");                                                                                                          
    exit(1);                                                                                                                   
  }                                                                                                                                                                                                                                                           
  travel_dir(argv[1]);                                                                                                         
  return 0;                                                                                                                    
}  

猜你喜欢

转载自blog.csdn.net/weixin_36750623/article/details/83037602
今日推荐