• Linux 文件IO


    1.文件IO 概念

    ①不带缓存的IO,read()| write()直接使用内核中的一个系统调用;
    ②文件IO 又称为低级磁盘IO,遵循POSIX标准;
    ③文件IO读写文件时,每次操作都会执行相关系统调用,优点:直接读写实际文件,缺点:频繁的系统调用会增加系统开销;

    2.文件描述符 (int fileno)

    范围:3~1024个文件描述符

    vim -t STDIN_FILENO
    
    • 1
    //在标准IO中,使用的最基本的文件描述符
    stdin   0
    stdout  1
    stderr  2
    
    • 1
    • 2
    • 3
    • 4

    打印标准IO中的stdin\stdout\stderr

    int main()
    {
    	FILE *fp;
    	fp=stdin;
    	printf("stdin:%d\n",fp->_fileno);
    	fp=stdout;
    	printf("stdout:%d\n",fp->_fileno);
    	fp=stderr;
    	printf("stderr:%d\n",fp->_fileno);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.int open(const char *pathname, int flags, mode_t mode);

    //参数:
    //pathname 文件名
    //flags O_RDONLY, O_WRONLY, O_RDWR,O_CLOEXEC, O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_TMPFILE, O_TRUNC
    //mode 文件掩码,修改文件权限 :实际权限为mode & ~umask (默认权限为0002)
    //返回值
    //成功返回文件描述符,失败返回-1;

    	   #include 
           #include 
           #include 
    
           int open(const char *pathname, int flags);//
           int open(const char *pathname, int flags, mode_t mode);
    
           int creat(const char *pathname, mode_t mode);
    
           int openat(int dirfd, const char *pathname, int flags);
           int openat(int dirfd, const char *pathname, int flags, mode_t mode);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    flags 用法,mode用法

    O_RDONLY        //只读
    O_WRONLY  		//只写
    O_RDWR			//读写
    O_APPEND        //追加
    O_CREAT			//创建
    O_CLOEXEC 		//把文件截断成0
    
    //常用组合
    "r" -> O_RDONLY
    "r+"-> O_RDWR
    
    "w" -> O_WRONLY|O_CREAT  |O_TRUNC,0666
    "w+"-> O_RDWR  | O_CREAT | O_TRUNC,0666
    
    "a" -> O_WRONLY | O_CREAT |O_APPEND,0666
    "a+"-> O_RDWR   | O_CREAT | O_APPEND,0666
    //实际权限为mode & ~umask ----->可以使用umask 命令查看umask
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    打开文件

    int main()
    {
    	int fd;
    	fd=open("log.txt",O_RDONLY);
    	if(fd<0)
    	{
    		perror("Fail to open");
    		return -1;
    	}
    	close(fd);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    4.ssize_t read(int fd, void *buf, size_t count);

    ssize_t write(int fd, const void *buf, size_t count);

    	#include 
        ssize_t read(int fd, void *buf, size_t count);
        ssize_t write(int fd, const void *buf, size_t count);
    //参数:
    //fp 文件描述符
    //buf,字符串首地址
    //count 传入字符串大小 :sizeof(buf);
    //返回值:
    //成功返回读到的字节数,失败返回-1;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    //从标准输入流输入,从标准输出流输出

    #include
    #include
    #include
    
    int main(int argc,const char *argv[])
    {
    	char buf[1024]={0};
    	int n=0;
    	while(1)
    	{
    		memset(buf,0,sizeof(buf));
    		n=read(STDIN_FILENO,buf,sizeof(buf));
    		if(strncmp(buf,"quit",4)
    		{
    			break;
    		}
    		write(STDOUT_FILENO,BUF,siezof(buf));
    		
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    5. off_t lseek(int fd, off_t offset, int whence);

    // 与offset /whence 两个参数与标准IO fseek()函数后两个参数使用方法一样

    	   #include 
           #include 
           off_t lseek(int fd, off_t offset, int whence);
    //参数
    //fd  文件描述符
    /offset  相对偏移量
    //whence  相对偏移起点
    //返回值  成功返回光标相对文件开头的偏移量
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    6. DIR *opendir(const char *name);//操作目录

    int closedir(DIR *dirp);

    struct dirent *readdir(DIR *dirp);

    //隐藏文件 d_name 开头为 '.'时;

     	   #include 
           #include 
    
           DIR *opendir(const char *name);//成功返回目录流指针,失败返回-1
           DIR *fdopendir(int fd);
           
     	   struct dirent *readdir(DIR *dirp);
    
    
     struct dirent {
                   ino_t          d_ino;       /* inode number */
                   off_t          d_off;       /* not an offset; see NOTES */
                   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]; /* filename */
               };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    //示例:对目录的打开关闭操作

    #include
    #include
    #include
    
    int main(int argc,const char* argv[])\
    {
    	DIR *dirp=NULL;//定义一个目录流指针
    	dirp=opendir("hello");//打开一个目录,返回目录流指针
    	if(dirp==NULL)
    	{
    		perror("Fail to opendir");
    		return -1;
    	}
    	closedir(dirp);//关闭一个目录
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    //示例:读取目录中的文件或目录
    //注意:readdir()函数读完所有目录和遇到错误时都会返回NULL;

    #include
    #include
    #include
    
    int main(int argc,const char* argv[])\
    {
    	DIR *dirp=NULL;//定义一个目录流指针
    	struct dirent *entry=NULL;
    	dirp=opendir("hello");//打开一个目录,返回目录流指针
    	if(dirp==NULL)
    	{
    		perror("Fail to opendir");
    		return -1;
    	}
    	errno=0;
    	while(1)
    	{
    		entry=readdir(dirp);
    		if(entry==NULL)//出错和到文件结尾,都会返回NULL,出错会置errno
    		{
    			if(errno!=0)//错误码被改变,说明读取出错
    			{
    				perror("Fail to readdir");
    				return -1;
    			}
    			else
    			{
    				printf("EOF");//到文件结尾了
    				break;
    			}
    		}
    		printf("d_name:%s\n",entry->d_name);
    	}
    	closedir(dirp);//关闭一个目录
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    //示例:遍历文件夹下某一种类型的文件

    #include
    #include
    #include
    
    int main(int argc,const char* argv[])\
    {
    	DIR *dirp=NULL;//定义一个目录流指针
    	struct dirent *entry=NULL;
    	dirp=opendir("hello");//打开一个目录,返回目录流指针
    	if(dirp==NULL)
    	{
    		perror("Fail to opendir");
    		return -1;
    	}
    	errno=0;
    	while(1)
    	{
    		entry=readdir(dirp);
    		if(entry==NULL)//出错和到文件结尾,都会返回NULL,出错会置errno
    		{
    			if(errno!=0)//错误码被改变,说明读取出错
    			{
    				perror("Fail to readdir");
    				return -1;
    			}
    			else
    			{
    				printf("EOF");//到文件结尾了
    				break;
    			}
    		}
    		if(entry->d_type==DT_REG)
    		{
    			printf("d_name:%s\n",entry->d_name);
    		}
    		//文件类型有七种:bcd-lps 及其宏定义
    		//b  块文件  	DT_BLK
    		//c  字符设备 	DT_CHR
    		//d  目录文件	DT_DIR
    		//-	 普通文件	DT_REG
    		//l  链接问价	DT_LNK
    		//p  有名管道文件DT_FIFO
    		//s  套接字文件  DT_SOCK
    	}
    	closedir(dirp);//关闭一个目录
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
  • 相关阅读:
    【Latex】使用技能站:(三)使用 Vscode 配置 LaTeX
    如何一键核实验证身份证的真伪?
    Perl 中的循环结构
    图09 --- 最小费用最大流问题
    Windows安装Vmware 虚拟机
    【pytorch】torch.gather()函数
    Java基础学习笔记-3
    单片机C语言实例:1、点亮LED的多种方法
    hugo个人博客 even主题美化记录
    一线大厂软件测试面试题及答案解析,2022最强版...
  • 原文地址:https://blog.csdn.net/jun8086/article/details/127722580