Home>

C language telldir () function:get the read position of the directory streamHeader file:

#include<dirent.h>

Define the function:

off_t telldir (dir * dir);

Function description:telldir () returns the current reading position of the dir directory stream. This return value represents the offset from the beginning of the directory file.The return value returns the next reading position. Returns -1 if an 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;d_name, offset);
  }
  closedir (dir);
}

Results of the:

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

c language seekdir () function:set the next read directory locationHeader file:

#include<dirent.h>

Define the function:

void seekdir (dir * dir, off_t offset);

Function description:seekdir () is used to set the current reading position of the parameter dir directory stream, When readdir () is called, reading starts from this new location. The offset parameter represents the offset from the beginning of the directory 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;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;d_name, offset);
  }
  closedir (dir);
}

carried out:

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
c
  • Previous Android programming method to achieve custom system menu background
  • Next Six ways to create AlertDialog in Android