第四节 stat函数和目录索引使用

本文主要介绍Linux系统编程中,如何使用stat函数获取文件属性,并探讨opendir、closedir和readdir进行目录操作的方法,是个人学习笔记。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

stat:功能:获取文件的属性

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>

    int stat(const char *path, struct stat *buf);
    int fstat(int fd, struct stat *buf);
    int lstat(const char *path, struct stat *buf);

    path:你要查看的文件的位置
    buf:存放文件属性的结构体,结构体解释如下:
    返回值:成功返回0,错误返回-1

    struct stat {
               dev_t     st_dev;     /* ID of device containing file */常规文件的ID
               ino_t     st_ino;     /* inode number */ 文件节点数字
               mode_t    st_mode;    /* protection */   文件的类型权限
               nlink_t   st_nlink;   /* number of hard links */ 硬链接数
               uid_t     st_uid;     /* user ID of owner */ 用户ID
               gid_t     st_gid;     /* group ID of owner */    组ID
               dev_t     st_rdev;    /* device ID (if special file) */特殊设备ID
               off_t     st_size;    /* total size, in bytes */ 文件大小,特殊文件判断不了
               blksize_t st_blksize; /* blocksize for file system I/O */块大小,一个页的大小
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */有几块数据,每一块的单位是512字节
               time_t    st_atime;   /* time of last access */      最新访问时间
               time_t    st_mtime;   /* time of last modification */    最新内容更改时间
               time_t    st_ctime;   /* time of last status change */   文件属性更改时间
           };


    如果你是一个常规文件,你想要获取设备节点号major和minor来获取 

    如果你想要获取文件类型:把st_mode传给以下函数就能判断文件类型,如果是则返回真,不是返回假
            S_ISREG(m)  is it a regular file?   //普通文件

            S_ISDIR(m)  directory?      目录文件

            S_ISCHR(m)  character device?   字符设备驱动文件

            S_ISBLK(m)  block device?       块设备驱动文件

            S_ISFIFO(m) FIFO (named pipe)?  管道文件

            S_ISLNK(m)  symbolic link? (Not in POSIX.1-1996.) 软链接文件

            S_ISSOCK(m) socket? (Not in POSIX.1-1996.)      套接字文件

opendir:打开一个目录文件(不是进入目录)

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

        DIR *opendir(const char *name);
        DIR *fdopendir(int fd);

        name:打开的目录具体位置

        返回值:返回一个目录操作指针
        失败返回NULL

closedir:关闭一个目录文件

        #include <sys/types.h>
        #include <dirent.h>
        int closedir(DIR *dirp);

        dirp:目录指针

        返回值:成功返回0,失败-1

readdir:

    一次调用只会读目录中一个文件的信息,如果你想要获取一个目录中的所有文件,
那你就要一直读
    #include <dirent.h>
    struct dirent *readdir(DIR *dirp);

    dirp:目录指针

    返回值:
        成功则获取到一个目录中一个成员的结构体,
        失败或者是没有文件可以读了,则返回NULL

    struct dirent {
                       ino_t          d_ino;       /* inode number */   //节点数(索引号)
                       off_t          d_off;       /* offset to the next dirent *///文件偏移量
                       unsigned short d_reclen;    /* length of this record */  //记录长度,比你的文件名要大
                       unsigned char  d_type;      /* type of file; not supported//文件类型
                                                      by all file system types */
                       char           d_name[256]; /* filename */   //文件名
                   };


    判断d_type是否为以下值便可以判断对应的文件类型

         DT_BLK      This is a block device.

         DT_CHR      This is a character device.

         DT_DIR      This is a directory.

         DT_FIFO     This is a named pipe (FIFO).

         DT_LNK      This is a symbolic link.

         DT_REG      This is a regular file.

         DT_SOCK     This is a UNIX domain socket.

         DT_UNKNOWN  The file type is unknown.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值