目录和文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35886943/article/details/89714382

目录:

打开目录函数

			#include<sys/types.h>
			#include<dirent.h>
			DIR *opendir(const char *name);
			DIR *fdopendir(int fd);
			两个函数返回:若成功则返回一个目录流,若出错则为NULL,并设置erron
				—— opendir用于打开一个给定路径的目录
				—— fdopendir用于打开一个和文件描述符绑定的目录

读取目录函数

			#include<dirent.h>
			struct dirent *readdir(DIR *dirp);
			—— 返回值是一个结构体指针,该结构体成员为描述该目录下的文件信息
			—— 到流结尾时返回NULL
			struct dirent{
				ino_t			d_ino;			//索引节点编号
				off_t			d_off;			//在目录文件中的偏移
				unsigned short 	d_reclen;		//文件名长度
				unsigned char	d_type;			//文件类型
				char			d_name[256];	//文件名
			};
			int chdir(const char *path);//将调用进程当前的工作目录改为path指定目录

文件:

  • 文件属性获取与修改相关的操作函数
    stat()fstat()lstat()
			#include<unistd.h>
			#include<sys/types.h>
			#include<sys/stat.h>
			
			int stat(const char *path,struct stat *buf);
			int fstat(int fd,struct stat *buf);
			int lstat(const char *path,struct stat *buf);
			三个函数的返回:若成功返回0,若出错返回-1,并且设置erron
			
			给定一个一个pathname的情况下:
				—— stat函数返回一个与此命名文件有关的信息结构
				—— fstat函数获得已在描述符filedes上打开的文件的有关信息
				—— lstat函数类似以stat,但是当命名的文件是一个符号链接时,lstat返回该
					符号链接的有关信息,不是由该符号连接引用的文件的信息
			

				
		stat结构体的定义
			struct stat  
			{  
				dev_t       st_dev;     /* ID of device containing file -文件所在设备的ID*/  
				ino_t       st_ino;     /* inode number -inode节点号*/
				mode_t      st_mode;    /* 文件的类型和存取的权限*/  
				nlink_t     st_nlink;   /* number of hard links -链向此文件的连接数(硬连接)*/  
				uid_t       st_uid;     /* user ID of owner -user id*/  
				gid_t       st_gid;     /* group ID of owner - group id*/  
				dev_t       st_rdev;    /* device ID (if special file) -设备号,针对设备文件*/  
				off_t       st_size;    /* total size, in bytes -文件大小,字节为单位*/  
				blksize_t   st_blksize; /* blocksize for filesystem I/O -系统块的大小*/  
				blkcnt_t    st_blocks;  /* number of 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 - 上次状态更改时间 */  
			};

		取得文件类型-检测宏
			宏的参数都是struct stat结构中的st_mode成员
			S_ISREG(m) 	//是否为普通文件
			S_ISDIR(m)	//是否为目录
			S_ISCHR(m)	//是否为字符设备
			S_ISBLK(m)	//是否为块设备
			S_ISFIFO(m)	//是否为管道
			S_ISLNK(m)	//是否为符号链接
			S_ISSOCK(m)	//是否为套接
			
			
			
		取得文件类型-st_mode	
			st_mode是用特征位来表示文件类型的,特征位的定义如下:
			
				S_IFMT			0170000     文件类型的位遮罩
				S_IFSOCK		0140000     socket
				S_IFLNK			0120000     符号链接(symbolic link)
				S_IFREG			0100000     一般文件
				S_IFBLK			0060000     区块装置(block device)
				S_IFDIR     	0040000     目录
				S_IFCHR    		0020000     字符装置(character device)
				S_IFIFO    		0010000     先进先出(fifo)
				
				S_ISUID     	0004000     文件的(set user-id on execution)位
				S_ISGID     	0002000     文件的(set group-id on execution)位
				S_ISVTX     	0001000     文件的sticky位
				
				S_IRWXU     	00700       文件所有者的遮罩值(即所有权限值)
				S_IRUSR     	00400       文件所有者具可读取权限
				S_IWUSR     	00200       文件所有者具可写入权限
				S_IXUSR     	00100       文件所有者具可执行权限
				S_IRWXG     	00070       用户组的遮罩值(即所有权限值)
				S_IRGRP     	00040       用户组具可读取权限
				S_IWGRP     	00020       用户组具可写入权限
				S_IXGRP     	00010       用户组具可执行权限
				S_IRWXO     	00007       其他用户的遮罩值(即所有权限值)
				S_IROTH     	00004       其他用户具可读取权限
				S_IWOTH     	00002       其他用户具可写入权限
				S_IXOTH     	00001       其他用户具可执行权限


				判断文件类型时,用对文件的st_mode的值与上面给出的值相与,再比较。比如:
				
				#include <sys/stat.h>
				#include <unistd.h>
				#include <stdio.h>
				 
				int main()
				{
					int abc;
					struct stat buf;
					stat("/home", &buf);
					abc = buf.st_mode & S_IFDIR;	//与对应的标志位相与
					if(abc == S_IFDIR)          	//结果与标志位比较
						printf("It's a directory.\n");
					return 0;
				}

猜你喜欢

转载自blog.csdn.net/qq_35886943/article/details/89714382
今日推荐