頭檔案
#include<sys/types.h>
#include<dirent.h>
函式原型
void seekdir(DIR *dirp,long int LOC);
函式說明
seekdir()用來設定參數dir目錄流目前的讀取位置,在調用readdir()時便從此新位置開始讀取。參數offset 代表距離目錄檔案開頭的偏移量。
返回值
錯誤代碼 EBADF 參數dir為無效的目錄流
範例
#include<sys/types.h>
#include<dirent.h>
#include<unistd.h>
main()
{
DIR * dir;
struct dirent * ptr;
int offset,offset_5,i=0;
dir=opendir(“/etc/rc.d”);
while((ptr = readdir(dir))!=NULL)
{
offset = telldir(dir);
if(++i = =5) offset_5 =offset;
printf(“d_name :%s offset :%d \n”,ptr->d_name,offset);
}
seekdir(dir offset_5);
printf(“Readdir again!\n”);
while((ptr = readdir(dir))!=NULL)
{
offset = telldir(dir);
printf(“d_name :%s offset :%d\n”,ptr->d_name.offset);
}
closedir(dir);
}
執行
d_name : . offset :12
d_name : .. offset:24
d_name : init.d offset 40
d_name : rc0.d offset :56
d_name :rc1.d offset :72
d_name:rc2.d offset :88
d_name:rc3.d offset 104
d_name:rc4.d offset:120
d_name:rc5.d offset:136
d_name:rc6.d offset:152
d_name:rc offset 164
d_name:rc.local offset :180
d_name:rc.sysinit offset :4096
readdir again!
d_name:rc2.d offset :88
d_name:rc3.d offset 104
d_name:rc4.d offset:120
d_name:rc5.d offset:136
d_name:rc6.d offset:152
d_name:rc offset 164
d_name:rc.local offset :180
d_name:rc.sysinit offset :4096