Linux目录io函数

stat结构体

struct stat 结构体是用来保存文件属性信息的

struct stat {
    dev_t     st_dev;         /* ID of device containing file */
    容纳该文件的那个设备的设备号
    ino_t     st_ino;         /* Inode number */
    该文件的 inode号-+
    mode_t    st_mode;        /* File type and mode */
    文件的类型和权限, mode_t 是整型,st_mode是用位域实现的
	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;     /* Block size for filesystem I/O */
    块大小(块也是一个存储单位,一般来说是512字节,根据不同的硬件设备有所不同)
    blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */
    文件有多少块
    struct timespec st_atim;  /* Time of last access */
    最后访问文件内容的时间
    struct timespec st_mtim;  /* Time of last modification */
    最后修改文件内容的时间
    struct timespec st_ctim;  /* Time of last status change */
    最后修改文件属性的时间

#define st_atime st_atim.tv_sec      /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec       
};

mode_t

系统了提供了一些宏,用来帮我们解析 (xx是 struct stat变量)

S_ISREG(xx.st_mode)  ->帮我们判断为普通文件的那个bit是否为1该宏返回真说明该文件是普通文件,否则不是普通文件
S_ISDIR(xx.st_mode) 该宏返回真说明该文件是目录,否则不是目录
S_ISCHR(xx.st_mode) 该宏返回真说明该文件是字符设备文件,否则不是字符设备文件
S_ISBLK(xx.st_mode) 该宏返回真说明该文件是块设备文件,否则不是块设备文件
S_ISLNK(xx.st_mode) 该宏返回真说明该文件是链接文件,否则不是链接文件
S_ISFIFO(xx.st_mode) 该宏返回真说明该文件是管道文件,否则不是管道文件
S_ISSOCK(xx.st_mode) 该宏返回真说明该文件是 socket文件,否则不是

(sb是struct stat变量)
switch (sb.st_mode & S_IFMT) {
    case S_IFBLK:  printf("block device\n");            		break;
    case S_IFCHR:  printf("character device\n");        		break;
    case S_IFDIR:  printf("directory\n");               		break;
    case S_IFIFO:  printf("FIFO/pipe\n");               		break;
    case S_IFLNK:  printf("symlink\n");                 		break;
    case S_IFREG:  printf("regular file\n");            		break;
    case S_IFSOCK: printf("socket\n");                  		break;
     default:       printf("unknown?\n");                		break;
}

不同的权限对应着不同的宏
S_IRUSR     S_IWUSR  S_IXUSR
S_IRGRP     S_IWGRP  S_IXGRP
S_IROTH     S_IWOTH  S_IXOTH

        if(xx.st_mode & S_IRUSR)
        {
        说明有用户可读权限
        }
		其它的也是如此

stat函数

int stat(const char *pathname, struct stat *statbuf);
	pathname:文件的路径名
	statbuf: struct stat结构体指针,指向的结构体用来保存获取到的文件属性信息
     	成功返回0 
     	失败返回-1,同时errno被设置

fstat函数

 int fstat(int fd, struct stat *statbuf);
        功能和 stat一样,第一个参数不是传路径名,而是文件描述符

lstat函数

int lstat(const char *pathname, struct stat *statbuf); 
    lstat功能用法和 stat一样,区别在于。
    如果这个文件是软链接文件,stat获取的是软链接指向的那个文件的属性
    lstat获取的是软链接文件本身的属性

目录IO

opendir函数

DIR *opendir(const char *name);
    name:目录的路径名
    返回值:
          失败返回 NULL,同时errno被设置
          成功返回指向目录的指针 (DIR 类型的指针)
DIR *fdopendir(int fd);
    fd:目录的文件描述符,意味着先要用 open函数打开这个目录(注意open打开目录只能是只读权限)
        返回值一样

readdir函数

#include <dirent.h>

struct dirent *readdir(DIR *dirp);
struct dirent {
    	ino_t          d_ino;       /* Inode number */ 
    	inode 号
        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 */
    文件或者子目录名字
};

closedir函数

struct dirent {
    ino_t          d_ino;       /* Inode number */ 
    inode 号
    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 */
    文件或者子目录名字
};

unlink 删除一个普通文件

int unlink(const char *pathname);

rmdir 删除空目录

int rmdir(const char *pathname);

remove 删除一个普通文件或者一个空目录

int remove(const char *pathname);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值