统计信息在while循环内不动态

时间:2018-07-31 00:58:20

标签: c

每次迭代while循环时,我都会为* fd分配一个不同的值,但是看来stat信息不是动态的。目的是获取作为命令行参数传递的目录,循环浏览该目录中的文件,并打印相关的统计信息。打印的文件名是动态的,但统计信息不是。

有人可以对此提供任何见识吗?

 while ((pDirent = readdir(pDir)) != NULL)
 {

     char *fd = pDirent->d_name;
     struct stat *buf;

     buf = malloc(sizeof(struct stat));

     stat(fd, buf);

     if (pDirent->d_name[0] != '.' && (lSwitch || noSwitch))
     {
         int i=0;
         for (i = 0; i<4; i++)
         {
             printf("%s", " ");
         }
         printf("%s", pDirent->d_name);
         if (lSwitch)
         {
             //  print   the file    type
             if (S_ISLNK(buf->st_mode))
                printf(" L ");
             else if (S_ISFIFO(buf->st_mode))
                printf(" P ");
             else if (S_ISREG(buf->st_mode))
                printf(" F ");
             else if (S_ISDIR(buf->st_mode))
                printf(" D ");
             else
                printf(" L ");
             printf("number  of  512 blocks  is  %ld\n", buf->st_blocks);

         }
         printf("\n");
     }

     free(buf);
 }

0 个答案:

没有答案