LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 810|回复: 0

关于stat结构的一个奇怪问题

[复制链接]
发表于 2011-3-13 17:37:02 | 显示全部楼层 |阅读模式
debian6.0

我使用了如下程序片断:
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <unistd.h>

  5. int main()
  6. {
  7.         struct stat statbuf;

  8.         if (!stat("./ttt", &statbuf)) {
  9.                 printf("%x\n%d\n", statbuf.st_dev, statbuf.st_ino);
  10.         }

  11.         return(0);
  12. }
复制代码

ttt是a.out相同目录下的一个文本文件。
用stat获得其文件信息。
用printf打印st_dev和st_ino,发现我用这个程序始,st_ino始终被打印为0。
然后我使用了如下代码:
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <unistd.h>

  5. int main()
  6. {
  7.         struct stat statbuf;

  8.         if (!stat("./ttt", &statbuf)) {
  9.                 printf("%x\n", statbuf.st_dev);
  10.                 printf("%d\n", statbuf.st_ino);
  11.         }

  12.         return(0);
  13. }
复制代码

此时st_ino可以被正确输出。

为了测试,我又使用了如下代码:
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <unistd.h>

  5. int main()
  6. {
  7.         struct stat statbuf;
  8.         int tmp;

  9.         if (!stat("./ttt", &statbuf)) {
  10.                 tmp = statbuf.st_ino;
  11.                 printf("%x\n%d\n", statbuf.st_dev, statbuf.st_ino);
  12.                 printf("%d\n", tmp);
  13.         }

  14.         return(0);
  15. }
复制代码

此时tmp的值为真确的st_ino值,而第一个printf的st_ino仍然被输出为0。

请问这是什么原因呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表