• linux下文件和目录


    (1)普通文件(regular file):这是最常用的文件类型,这种文件包含了某种形式的数据,文件内容的解释由处理该文件的应用程序进行。


    (2)目录文件(directory file):这种文件包含了其他文件的 名字以及指向这些文件有关信息的指针。对一个目录文件具有读权限的进程,都可以读该目录的内容,但只有内核可以  直接写目录文件。


    (3)块特殊文件(block special file):这种类型的文件提供对设备(如磁盘)带缓冲的访问,每次访问一固定长度为单位进行。


    (4)字符特殊文件(character special file):这种类型的文件提供对设备不带缓冲的访问,每次访问长度可变。系统中的所有设备,要么是字符特殊文件,要么是块特殊文件。


    (5)FIFO:这种文件用于进程间通信,也叫做有命名管道。


    (6)套接字(socket):这种类型的文件用于进程间的网络通信。套接字也可用于一台宿主机上进程间的非网络通信。


    (7)符号连接(symbolic link):这种类型的文件指向另一个文件。

    函数描述:
    1、头文件:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    2、函数原型:
    int stat(const char *path, struct stat *buf);
    int lstat(const char *path, struct stat *buf);
    3、参数:
    a、path:文件名
    b、buf:是一个(struct stat)的指针

    下面是struct stat结构体的详细介绍:

     struct stat {
    						   dev_t     st_dev;     /* ID of device containing file */
    						   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 */
    						   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; /* blocksize for file system I/O */
    						   blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
    						   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 */
    			            };
    

      

    4、返回值:
    成功:0
    失败:-1

    总结:这里stat和lstat有点区别的,lstat可以看到符号连接(symbokic link),而stat却不可以,下面我用代码具体给出来区别的地方。

    文件类型包括在stat结构的st_mode成员中,下面是这7种文件类型的判断方法:
        宏                                  文件类型
    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 [FIFO (named pipe)?]
    S_ISLNK(m)        符号链接 [symbolic link? (Not in POSIX.1-1996.)]
    S_ISSOCK(m)     套接字 [socket? (Not in POSIX.1-1996.)]

    判断例子:

           struct stat fileinfo;
           stat("text.txt",&fileinfo); //text.txt为普通文件
           if(S_ISREG(fileinfo.st_mode))
           {
             printf("the file is regular file
    ");
           }

     好了,说了这么多,直接上代码看看具体是怎么判断文件的类型的:

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc,char *argv[])
    {
        if(argc !=5 )
        {
            printf("argc must equal to 5!
    ");
            exit(1);
        }     
        struct stat fileinfo;   
        int i;
        for(i=1;i<5;i++)
        {
            bzero(&fileinfo,sizeof(fileinfo));
            //stat(argv[i],&fileinfo); //这里stat和lstat有点的区别,lstat可以产看到符号连接(symbokic link),而stat却不可以
            lstat(argv[i],&fileinfo);
            if(S_ISREG(fileinfo.st_mode)) //普通文件
            {
                printf("%s file is regular file
    ",argv[i]);
            }
            else if(S_ISDIR(fileinfo.st_mode)) //目录
            {
                printf("%s file is directory file
    ",argv[i]);
            }        
            else if(S_ISCHR(fileinfo.st_mode)) //字符特殊文件
            {
                printf("%s file is character device file
    ",argv[i]);
            }
            else if(S_ISBLK(fileinfo.st_mode)) //块特殊文件
            {
                printf("%s file is block device file
    ",argv[i]);
            }
            else if(S_ISFIFO(fileinfo.st_mode)) //FIFO或管道
            {
                printf("%s file is FIFO
    ",argv[i]);
            }        
            else if(S_ISLNK(fileinfo.st_mode))  //符号连接
            {
                printf("%s file is symbokic link
    ",argv[1]);
            }        
            else if(S_ISSOCK(fileinfo.st_mode)) //套接字
            {
                printf("%s file is socket
    ",argv[i]);
            }        
            else
            {
                printf(" %s not a file name
    ",argv[i]);
            }
        }    
        return 0;
    }

     lstat和stat的区别:

    代码中注释掉://stat(argv[i],&fileinfo); 即是用到lstat函数时终端的输出如下

    代码中注释掉:lstat(argv[i],&fileinfo);即是用到stat函数终端的输出如下

    鉴于本人才疏学浅,所以其中不免有遗漏或者错误,恳请各位博友批评指正。
  • 相关阅读:
    HBase原理和架构
    Hive UDF作业
    Hive性能调优
    hive
    Netty4.0学习笔记系列之一:Server与Client的通讯
    JAVA NIO 简介(转)
    设计模式之观察者模式(Observer Pattern)
    设计模式之装饰者模式(Decorator Pattern)
    mysql存储过程写法—动态参数运用
    hashCode() 和equals() 区别和作用
  • 原文地址:https://www.cnblogs.com/wurenzhong/p/7507914.html
Copyright © 2020-2023  润新知