Explain the usage of telldir () function and Seekdir () function in C language _c language

Source: Internet
Author: User

C language Telldir () function: Gets the read location of the directory stream
header file:

 #include <dirent.h>

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:

#include <dirent.h>

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.