linux遍历目录下的文件

来源:互联网 发布:移动公司网络维修电话 编辑:程序博客网 时间:2024/05/21 22:36
  #include   <stdio.h>   
  #include   <stdlib.h>   
    
  #include   <sys/types.h>   
  #include   <dirent.h>   
    
  int main(int argc,  char*argv[])   
  {   
unsigned int loop=0;
char *dot= ".";
char *dotdot ="..";
DIR *dp;   
  struct   dirent   *dirp;   
    
  if(argc != 2)   
  {   
  printf("not   enough   arguments!   exit!\n");   
  exit(0);   
  }   
    
  if((dp =opendir(argv[1]))== NULL)   
  {   
  printf("can't   open   %s!\n",argv[1]);   
  exit(0);   
  }   
    
  while((dirp = readdir(dp))!=NULL)
{   
 if( strcmp(dirp->d_name, dot)
 && strcmp(dirp->d_name, dotdot) ) 
 {


 printf("%d ,%s\n",dirp->d_off, dirp->d_name);   
 loop++;
 }
}   
  closedir(dp);  
printf("%u",loop); 
  }