opendir,readdir,closedir

 结构体dirent:

struct dirent
{
    ino_t    d_ino;  //inode number
    off_t        d_off;    //offset to the next diret
    unsigned short d_reclen;    //length of this record
    unsigned short d_type;        //type of file
    char         d_name[256];    //filename
}

结构体DIR

struct __dirstream{
    void *__fd;                /* `struct hurd_fd' pointer for descriptor.   */
    char *__data;            /* Directory block.   */
    int __entry_data;    /* Entry number `__data' corresponds to.   */
    char *__ptr;               /* Current pointer into the block.   */
    int __entry_ptr;        /* Entry number `__ptr' corresponds to.   */
    size_t __allocation;      /* Space allocated for the block.   */
    size_t __size;                /* Total valid data in the block.   */
    __libc_lock_define (, __lock)          /* Mutex lock for this structure.   */
} DIR;

opendir函数

#include<diret.h>
DIR* opendir(const char *path)

打开一个目录,在失败的时候返回一个空指针

opendir函数打开一个与给定名的目录名name相对应的目录流,并返回一个指向该目录流的指针。打开后,该目录流指向了目录中的第一个目录项。若打开成功,则返回指向目录流的指针打开失败,返回NULL,并设置相应的错误代码errno

readdir函数

struct dirent *readdir(DIR *dir)

readdir函数返回一个指向dirent结构体的指针,该结构体代表了由dir指向的目录流中的下一个目录项;如果读到end-of-file或者出现错误,那么返回NULL。

该函数返回的值会被后续调用的(针对同一目录流)readdir函数返回值所覆盖

函数调用成功会返回一个指向dirent结构体的指针,失败时或读到end-of-life时,返回NULL,并且设置相应的错误代码errno。

closedir函数

int closedir(DIR *dir);

closedir函数关闭与指针dir相联系的目录流,关闭后目录流描述符dir不可再用。函数成功时返回0,失败时返回-1并设置了相应的错误代码errno

chdir、fchdir函数

int chdir(const char *path)
int fchdir(int fd)

chdir函数改变当前的工作目录位path指定的目录。

fchdir函数和chdir功能一样,唯一的区别就是fchdir所改变的工作目录由打开的文件描述符所指定。

原文地址:https://www.cnblogs.com/wanghao-boke/p/11599695.html