您的位置:首页 > 运维架构 > Linux

linux遍历目录下的文件

2013-08-06 01:04 148 查看
  #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); 

  }   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: