曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 5576|回复: 0
打印 上一主题 下一主题

telldir, seekdir

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2009-2-12 22:45:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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() 重新定位后,目录流就从 .. 这里开始扫描了。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-7 05:15 , Processed in 0.075711 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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