• 文件属性和目录


    1、文件属性

    #include

    #include

    #include

    int stat(const char *pathname, struct stat *statbuf);  //获取文件属性

    struct stat {

        dev_t     st_dev;         /* ID of device containing file */

        ino_t     st_ino;         /* Inode number */

        mode_t    st_mode;        /* File type and mode */

        nlink_t   st_nlink;       /* Number of hard links */

        uid_t     st_uid;         /* User ID of owner */

        gid_t     st_gid;         /* Group ID of owner */

        dev_t     st_rdev;        /* Device ID (if special file) */

        off_t     st_size;        /* Total size, in bytes */

        blksize_t st_blksize;     /* Block size for filesystem I/O */

        blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

    };

    注:使用下面的宏函数可以判断文件的类型:参数为struct stat的st_mode成员

    S_ISREG(st_mode);  --- 判断是否是普通文件

    S_ISDIR(st_mode)  directory?

    S_ISCHR(st_mode)  character device?

    S_ISBLK(st_mode)  block device?

    S_ISFIFO(st_mode) FIFO (named pipe)?

    S_ISLNK(st_mode)  symbolic link? (Not in POSIX.1-1996.)

    S_ISSOCK(st_mode) socket? (Not in POSIX.1-1996.)

    ```

    2、目录

    int system(char *commad);  //执行shell命令

    int chdir(char *path); //切换当前的工作路径,类似与cd命令

    参数:

        path: 要切换的路径

    返回值:

          成功: 0

        失败: -1, 并设置errno

    #include

    #include

    DIR *opendir(const char *name);   //打开目录

    参数:

        name: 目录名

    返回值:

        成功:目录流指针

        失败:NULL, 并设置errno

    int closedir(DIR *dirp);   //关闭目录

    参数:

        dirp: 目录流指针

    struct dirent *readdir(DIR *dirp);  //读取目录

    struct dirent {

        ino_t          d_ino;       /* Inode number */

        off_t          d_off;       /* Not an offset; see below */

        unsigned short d_reclen;    /* Length of this record */

        unsigned char  d_type;      /* Type of file; not supported

                                                  by all filesystem types */

        char           d_name[256]; /* Null-terminated filename */

    };

    参数: 目录流指针

    返回值:

       读取的文件dirent结构体指针

        当读取到目录流结束时,返回NULL

    代码如下:

    #include
    #include

    int main(int argc, char *argv[])

        int ret;
        struct stat buf;
        if (argc < 2)
        {
            fprintf(stderr, "Usage: %s \n", argv[0]);
            return -1;
        }

        ret = stat(argv[1], &buf); //获取文件属性
        if (ret < 0)
        {
            perror("stat");
            return -1;
        }

        if (S_ISREG(buf.st_mode)) //判断文件的类型是否是普通文件 
            printf("%s is regular file!\n", argv[1]);
        else
        {
            printf("%s isn't a regular file!\n", argv[1]);
            return -1;
        }

        printf("size: %ld\n", buf.st_size);  //文件的大小

        return 0;

     

  • 相关阅读:
    ts的装饰器
    React Router 路由守卫
    【linux】chmod命令
    Windows与网络基础-18-注册表基础
    ESIM:Enhanced LSTM for Natural Language Inference
    数据结构之队列
    对象创造模式
    java基于Springboot+vue 的在线药品销售商城购药管理系统 elementui
    【OpenDDS开发指南V3.20】第四章:条件和监听
    Activiti监听器
  • 原文地址:https://blog.csdn.net/qq_63626307/article/details/126450703