设为首页 加入收藏

TOP

linux下文件和目录(一)
2017-10-13 10:37:21 】 浏览:5145
Tags: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\n");
       }

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

#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!\n");
        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\n",argv[i]);
        }
        else if(S_ISDIR(fileinfo.st_mode)) //目录
        {
            printf("%s file is directory file\n",argv[i]);
        }        
        else if(S_ISCHR(fileinfo.st_mode)) //字符特殊文件
        {
            printf("%s file is character device file\n",argv[i]);
        }
        else if(S_ISBLK(fileinfo.st_mode)) //块特殊文件
        {
            printf("%s file is block device file\n&q
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇集群上配置Keepalived实现负载均衡 下一篇awk知识点全回顾

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目