• 文件属性和目录


    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;

     

  • 相关阅读:
    计算机操作系统-笔记
    API 工程化分享
    opencv-python读写中文路径问题的解决
    微信公众号运营的那些实用技巧,你了解多少?
    Promise
    (附源码)springboot公益慈善管理系统 毕业设计 281454
    好音质蓝牙耳机有哪些?音质最好的耳机排名
    自研芯片重构云上算力革新,满足用户所有负载的需求!
    idea安装MyBatisX插件,没有效果
    vue3中异步变同步的方式以及注意点
  • 原文地址:https://blog.csdn.net/qq_63626307/article/details/126450703