文件IO中的目录操作

opendir函数

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
功能:打开一个目录,获取目录流指针
参数:
	name:目录名
返回值:
	DIR是用来描述一个打开的目录文件的结构体类型,成功时返回目录流指针;出错时返回NULL

readdir函数

#include <dirent.h>
struct dirent *readdir(DIR *dirp);

功能:读取目录的信息
参数:
	dirp:目录流指针
返回值:
	成功:成功时返回目录流dirp中下一个目录项;
	失败:NULL
	如果目录的内容都读取完毕,则返回NULL	
	
struct dirent是用来描述目录流中一个目录项的结构体类型
struct dirent {
ino_t          d_ino;       /* inode number */
off_t          d_off;       /* offset to the next dirent */
unsigned short d_reclen;    /* length of this record */
unsigned char  d_type;      /* type of file; not supported
           by all file system types */
char           d_name[256]; /* filename */
};

closedir( )

#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
功能:关闭一个目录流指针
参数:
	dirp:目录流指针
返回值:
	成功:0
	失败:-1

chmod/fchmod函数

#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);
功能:
		修改文件访问权限
参数:
	    成功时返回0;出错时返回EOF
root和文件所有者能修改文件的访问权限

chmod("a.txt", 0666);

stat/lstat/fstat函数


#include <sys/stat.h>

int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);

功能:stat/lstat/fstat函数用来获取文件属性
返回值:
	成功时返回0;出错时返回EOF
如果path是符号链接stat获取的是目标文件的属性;而lstat获取的是链接文件的属性

文件信息的结构体


struct stat {
//dev_t	 st_dev;     /* ID of device containing file */   文件所在设备的ID
//ino_t	 st_ino;     /* inode number */    inode节点号
mode_t	 st_mode;    /* protection */  模式(文件类型、访问权限)
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) */ 设备号,针对设备文件
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 */  文件创建的时间
};		

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 (named pipe)?

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

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


The following flags are defined for the st_mode field:

S_IFMT     0170000   bit mask for the file type bit fields
S_IFSOCK   0140000   socket
S_IFLNK    0120000   symbolic link
S_IFREG    0100000   regular file
S_IFBLK    0060000   block device
S_IFDIR    0040000   directory
S_IFCHR    0020000   character device
S_IFIFO    0010000   FIFO
S_ISUID    0004000   set-user-ID bit
S_ISGID    0002000   set-group-ID bit (see below)
S_ISVTX    0001000   sticky bit (see below)
S_IRWXU    00700     mask for file owner permissions
S_IRUSR    00400     owner has read permission
S_IWUSR    00200     owner has write permission
S_IXUSR    00100     owner has execute permission
S_IRWXG    00070     mask for group permissions
S_IRGRP    00040     group has read permission
S_IWGRP    00020     group has write permission
S_IXGRP    00010     group has execute permission
S_IRWXO    00007     mask for permissions for others (not in group)
S_IROTH    00004     others have read permission
S_IWOTH    00002     others have write permission
S_IXOTH    00001     others have execute permission
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值