【发布时间】:2015-09-15 11:14:42
【问题描述】:
程序:
#include<stdio.h>
#include<dirent.h>
void main()
{
int i=0;
DIR *dir=opendir("dir");
struct dirent *dent;
while((dent=readdir(dir))!=NULL){
printf("Filename: %s\t\t Location in Directory Stream: %ld\n",
dent->d_name,telldir(dir));
}
}
输出:
$ ./a.out
Filename: b.txt Location in Directory Stream: 32
Filename: a.txt Location in Directory Stream: 64
Filename: d.c Location in Directory Stream: 96
Filename: . Location in Directory Stream: 128
Filename: test Location in Directory Stream: 160
Filename: .. Location in Directory Stream: 192
$
在上面的程序中,telldir() 函数的返回值是 32 的倍数。根据 telldir() 的手册页参考
是“返回目录流中的当前位置”。所以,我希望目录包含 5 个文件,所以第一次调用 telldir() 返回 1 和
在下一个调用它返回 2。但是这里的输出是 32 的倍数。为什么输出是这样的?而telldir()函数为什么会返回这样的值呢?
【问题讨论】:
-
您可能需要阅读this manual page 中的备注部分。
-
类似于手册页。此处未提及更多其他信息。
-
来自linked manual page:“现代文件系统使用树或散列结构,而不是平面表来表示目录。在这样的文件系统上,由telldir()返回的值(并在内部由readdir( 3)) 是一个“cookie”,实现使用它来导出目录中的位置。应用程序应严格将此视为不透明值,对其内容不做任何假设。”
telldir函数返回一个值,这个值是什么无关紧要,不要用特定的方式解释。