C language Telldir () function: Gets the read location of the directory stream
header file:
To define a function:
off_t Telldir (DIR *dir);
Function Description: Telldir () returns the current read location of the parameter Dir directory stream. This return value represents the return value of the offset from the beginning of the catalog file to the next read position and returns-1 if the error occurs.
Error code: EBADF parameter dir is an invalid directory stream.
Example
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
main ()
{
DIR *dir;
struct dirent *ptr;
int offset;
dir = Opendir ("/etc/rc.d");
while (ptr = Readdir (dir))!= NULL)
{
offset = telldir (dir);
printf ("D_name:%s Offset:%d\n", ptr->d_name, offset);
}
Closedir (dir);
Execution results:
D_name:. Offset:12
d_name:. offset:24
d_name:init.d offset d_name:rc0.d
offset:56 d_name:rc1.d offse
t:72
d_name:rc2.d offset:88
d_name:rc3.d offset:104
d_name:rc4.d offset:120
d_name:rc5.d OFFSE t:136
d_name:rc6.d offset:152
d_name:rc offset:164
d_name:rc.local offset:180
D_name:rc.sys Init offset:4096
C language Seekdir () function: Set the location of the next read directory
header file:
To define a function:
void Seekdir (dir * dir, off_t offset);
Function Description: Seekdir () is used to set the current read location of the parameter Dir directory stream, and start reading from this new location when Readdir () is invoked. The parameter offset represents the offset from the beginning of the catalog file.
Error code: EBADF parameter dir is an invalid directory stream.
Example
#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);
Perform:
D_name:. Offset:12
d_name:. offset:24
d_name:init.d offset d_name:rc0.d
offset:56 d_name:rc1.d offse
t:72
d_name:rc2.d offset:88
d_name:rc3.d offset:104
d_name:rc4.d offset:120
d_name:rc5.d OFFSE t:136
d_name:rc6.d offset:152
d_name:rc offset:164
d_name:rc.local offset:180
D_name:rc.sys Init 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<
C17/>D_NAME:RC6.D offset:152
d_name:rc offset:164
d_name:rc.local offset:180 d_name:rc.sysinit
o ffset:4096