Ŀ¼ÔÚÀûÓÃreaddir()½øÐзÃÎÊʱ£¬»ñȡĿ¼ÖжÔÏóµÄ˳Ðò²¢²»ÊÇͨ¹ýllÔÚÖÕ¶ËÉÏ¿´µ½µÄ˳Ðò¡£
seekdir¾ßÌåÓ÷¨£º
seekdir(ÉèÖÃÏ»ضÁȡĿ¼µÄλÖã©¡£
±íÍ·Îļþ£º#include <dirent.h>
º¯ÊýÐÎʽ£ºvoid seekdir(DIR *dir,off_t offset);
º¯Êý˵Ã÷£ºseekdir£¨£©ÓÃÀ´ÉèÖòÎÊýdirĿ¼Á÷Ŀ¼µÄ¶ÁȡλÖã¬ÔÚµ÷Óà readir()ʱ£¬±ã´Ó´ËÐÂλÖÿªÊ¼¶ÁÈ¡¡£²ÎÊýoffset´ú±í¾àÀëĿ¼Îļþ¿ªÍ·µÄÆ«ÒÆÁ¿¡£
ʾÀý£º
#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;//Ŀ¼µÚ5¸ö¶ÔÏóÏà¶ÔÓÚĿ¼¿ªÍ·µÄÆ«ÒÆÁ¿
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);
}
//ÉÏÃæÀý×Óͨ¹ý¶à´Îreadir£¨£©£¬¿É»ñȡĿ¼µÚ5¸ö¶ÔÏóÏà¶ÔÓÚÆ«ÒÆÁ¿µÄλÖ㬵«Õâ¸ö¶ÔÏó²»Ò»¶¨¾ÍÊÇllÁгöµÄµÚ5¸ö¶ÔÏó¡£
±¸×¢£ºÏê¼ûËù·¢Ñ¹Ëõ°üÖÐ"linux cº¯Êý¿â²Î¿¼ÊÖ²á"¡£