|
telldir 函数返回一个记录目录流中当前位置的值.这个值可以用在seekdir中用来重置目录扫描到当前的位置.
用法:#include <sys/types.h>
#include <dirent.h>
long int telldir(DIR *dirp);
用法:#include <sys/types.h>
#include <dirent.h>
void seekdir(DIR *dirp, long int loc);
seekdir 函数设置由 dirp 给定的目录流里的目录条目指针. loc 用来设置位置, 可以从 telldir 函数获得.
示例程序:
#include <stdio.h>
#include <dirent.h>
#include <string.h>
int main()
{
DIR *dirp;
struct dirent *dp;
long int loc;
dirp = opendir ("./");
while ((dp = readdir(dirp)) != NULL) {
printf ("%s\n", dp->d_name);
if (strcmp (dp->d_name, "test.txt") == 0)
loc = telldir(dirp);
}
closedir (dirp);
printf ("----------------------new------------------\n");
dirp = opendir ("./");
seekdir (dirp, loc);
while ((dp = readdir(dirp)) != NULL)
printf ("%s\n", dp->d_name);
return (0);
} 运行与输出:./telldir
get_current_dir_name
readdir
mps.c
printf
fdopen.c
readdir.c
test.txt
..
mps
mps_test.c
.
dir_test
mps_test
fileno.c
fileno
printf.c
get_current_dir_name.c
telldir.c
telldir
fdopen
----------------------new------------------
..
mps
mps_test.c
.
dir_test
mps_test
fileno.c
fileno
printf.c
get_current_dir_name.c
telldir.c
telldir
fdopen 说明:
我们要在找到 test.txt 这个文件后,在此做好目录流的标记。实际上,在找到此文件后,目录流指针已经移动到test.txt文件的下一个位置 .. (上一层目录)处,所以标记是在此做。从分割线的下半部可以看出,当用 seekdir() 重新定位后,目录流就从 .. 这里开始扫描了。 |
|