Chinaunix首页 | 论坛 | 博客
  • 博客访问: 118463
  • 博文数量: 42
  • 博客积分: 916
  • 博客等级: 准尉
  • 技术积分: 420
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-16 10:01
文章分类

全部博文(42)

文章存档

2011年(42)

分类: C/C++

2011-08-09 16:16:35


  1. #include <stdio.h>
  2. #include <dirent.h>

  3. int main(int argc,char* argv[])
  4. {
  5.     DIR *dp;
  6.     struct dirent *dirp;
  7.     unsigned char c='d';
  8.     int i=0, offset=0;

  9.     if(argc != 2)
  10.     {
  11.         perror("please input directory!");
  12.         return 1;
  13.     }
  14.     if((dp = opendir(argv[1])) == NULL)
  15.     {
  16.         perror("can't open /home");
  17.         return 1;
  18.     }
  19.     while((dirp = readdir(dp)) != NULL)
  20.     {
  21.         #if 0
  22.         printf("%ld--",dirp->d_ino);
  23.         printf("%u--",dirp->d_reclen);
  24.         printf("%d--",dirp->d_off);
  25.         #endif
  26.         i++;
  27.         if(1 == i)
  28.             offset = telldir(dp);
  29.         printf("%d:%s\n",i,dirp->d_name);
  30.     } //到此目录流已经结束
  31.     printf("end\n");
  32.     
  33.     rewinddir(dp);//将目录流返回到开始处

  34.     printf("again\n");
  35.     while((dirp = readdir(dp)) != NULL)
  36.     {
  37.          printf("%s\n",dirp->d_name);
  38.     }

  39.     printf("this is telldir ,seekdir:\n");

  40.     seekdir(dp,offset);//将目录流定位到之前telldir的位置处,然后调用readdir向后读取

  41.     while((dirp = readdir(dp)) != NULL)
  42.     {
  43.         #if 0
  44.         printf("%ld--",dirp->d_ino);
  45.         printf("%u--",dirp->d_reclen);
  46.         printf("%d--",dirp->d_off);
  47.         #endif
  48.         printf("%s\n",dirp->d_name);
  49.     }

  50.     closedir(dp);

  51.     return 0;
  52. }

阅读(1057) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~