C language Fchdir () function: Change the current working directory
header file:
To define a function:
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