C language to change the contents of the relevant operational functions detailed _c language

Source: Internet
Author: User

C language Fchdir () function: Change the current working directory
header file:

 #include <unistd.h>

To define a function:

int fchdir (int fd);

Function Description: Fchdir () is used to change the current working directory to a parameter FD refers to the file descriptor.

Return value: Returns 0 for successful execution, failure returns-1, errno as error code.

Example

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include < unistd.h>
Main ()
{
  int fd;
  FD = open ("/tmp", o_rdonly);
  Fchdir (FD);
  printf ("Current working directory:%s \ n", GETCWD (null, NULL));
  Close (FD);
}

Perform:

Current working directory:/tmp

C language Rewinddir () function: Resets the location of the read directory to the beginning position
header file:

#include <sys/types.h>  #include <dirent.h>

To define a function:

void Rewinddir (DIR *dir);

Function Description: Rewinddir () is used to set the parameter dir directory stream the current read location is the original start of the read location.

Error code: EBADF dir is an invalid directory stream.

Example

#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
main ()
{
  dir * dir;
  struct dirent *ptr;
  dir = Opendir ("/etc/rc.d");
  while (ptr = Readdir (dir))!= NULL)
  {
    printf ("D_name:%s\n", ptr->d_name);
  }
  Rewinddir (dir);
  printf ("Readdir again!\n");
  while (ptr = Readdir (dir))!= NULL)
  {
    printf ("D_name:%s\n", ptr->d_name);
  }
  Closedir (dir);

Perform:

D_name:.
D_name:..
D_NAME:INIT.D
d_name:rc0.d
d_name:rc1.d
d_name:rc2.d
d_name:rc3.d
d_name:rc4.d : rc5.d
d_name:rc6.d
d_name:rc
d_name:rc.local
d_name:rc.sysinit
readdir D_name:.
D_name:..
D_NAME:INIT.D
d_name:rc0.d
d_name:rc1.d
d_name:rc2.d
d_name:rc3.d
d_name:rc4.d
D _NAME:RC5.D
d_name:rc6.d
d_name:rc
d_name:rc.local
d_name:rc.sysinit

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.