read和write函数的使用

都需要包含头文件: <unistd.h>

read系统函数从打开的设备或文件中读取数据,即将数据从外设上经过内核读到用户空间;write系统函数相反,向打开的设备或文件中写入数据,即将数据从用户空间(I/O缓冲)送到内核,然后刷到外设上。它们的函数原型如下:

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

ssize_t为有符号整型,size_t为无符号整型。fd为相应的文件描述符;buf为用户给定的数据缓冲区,该缓冲不是固定大小的,由count值决定其大小(用户给定,字节数)。如 read( fd , “hello” , 5 ); 此时的void *buf为char *类型。即count为请求读取的字节数(即buf的大小)。该函数的返回值为-1时,表示读取数据失败;返回值>0时,表示读出的字节数;返回值等于0时,表示已经读完了,因此没有数据可读了。

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

buf为需要输出的缓冲区,由用户给定。cout为最大输出字节数(buf的大小,字节数)。返回值为-1时,表示写入失败;>=0时,表示写入的字节数。

以上两个缓冲区buf都是用户空间的地址,但是与I/O缓冲区不一样,后者是规定的,前者是用户自己指定的。

思考:利用readwrite每次读或写1Byte与利用getsputs每次读或写1Byte哪一种方式速度更快?

read和write函数为Linux系统函数,其缓冲区由用户来维护,即用户指定其大小,从而每次要将用户空间的数据送到内核或从内核送到用户空间的数据大小是由用户来规定的(count);而gets和puts为C库函数,其I/O缓冲区由库函数自己维护,大小为8Byte,因此平均上,每传送8个Byte的数据量才会操作一次内核。综上,以上两者在读写数据时,显然read、write函数每次从用户空间读1Byte数据是就会操作一次内核(系统调用)(,开销更大,速度会更慢。而puts和gets函数速度更快。 当然,用户可以指定read和write的count参数,来增大其缓冲区大小,从而提高其读写速度,使其比puts和gets更快(对大文件来说,效果更加明显)。

下面举例说明read和write的用法:

 

//将一个文件(english.txt)的内容读到另一个文件(writefile.txt

[root@localhost work]# vim rdwr.c

[root@localhost work]# ls

english.txt  rdwr.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>int main( )
{int fd;fd = open("english.txt",O_RDONLY);  //以只读方式打开printf("open readfile's fd=%d\n",fd);if( fd == -1 ){perror(" open english.txt " );exit(1);}int fd1;char buff[1024] = { 0 };    //定义一个缓冲区fd1 = read( fd,buff,sizeof(buff) );   //将数据读出到缓冲区printf("read readfile's fd1=%d\n",fd1);if( fd1 == -1 ){perror(" read english.txt " );exit(1);}int fd2;fd2 = open( "writefile.txt" ,O_WRONLY | O_CREAT | O_EXCL,0664);  //创建一个写文件,并以只写的方式打开,如果文件创建的文件存在,则结束printf("open writefile's fd2=%d\n",fd2);if ( fd2 == -1 ){perror( "creat file" );exit(1);}int ret;while( fd1 ){ret = write( fd2,buff,fd1);  //将读出的数据写进另一个新文件if( ret == -1 ){perror( "write file");exit(1);}fd1 = read( fd,buff,1024);  //再次读数据到缓冲区if( fd1 == -1 ){perror(" read english.txt " );exit(1);}}int qw1;int qw2;qw1=close(fd);  //关闭文件if( ret == -1 ){perror( "close readfile");exit(1);}qw2=close(fd2);if( ret == -1 ){perror( "close writefile");exit(1);}return 0;
}

 [root@localhost work]# gcc -pipe -pedantic -Wall -ggdb3 rdwr.c -o rdwr

[root@localhost work]# ./rdwr

open readfile's fd=3

read readfile's fd1=1024

open writefile's fd2=4

[root@localhost work]# ls

english.txt  rdwr  rdwr.c  writefile.txt

[root@localhost work]# ll writefile.txt

-rwxrwxrwx. 1 root root 109055 Mar 19 11:39 writefile.txt

[root@localhost work]# ll english.txt

-rwxrwxrwx. 1 root root 109055 Mar 19 10:30 english.txt  //两文件大小一样

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/385404.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1091. Acute Stroke (30)

One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core. Input Speci…

lseek函数的使用

需要包含头文件&#xff1a;<sys/types.h> <unistd.h> off_t lseek(int fd, off_t offset, int whence)&#xff1b; 函数原型 函数功能&#xff1a;移动文件读写指针&#xff1b;获取文件长度&#xff1b;拓展文件空间。 在使用该函数之前需要将文件打开&…

19. 删除链表的倒数第N个节点

给定一个链表&#xff0c;删除链表的倒数第 n 个节点&#xff0c;并且返回链表的头结点。 示例&#xff1a; 给定一个链表: 1->2->3->4->5, 和 n 2. 当删除了倒数第二个节点后&#xff0c;链表变为 1->2->3->5. 说明&#xff1a; 给定的 n 保证是有效的。…

文件操作相关的系统函数

重点学习&#xff1a;stat&#xff08;fstat、lstat 获取文件属性&#xff09;、access&#xff08;测试指定文件是否拥有某种权限&#xff09;、chmod&#xff08;改变文件的权限&#xff09;、chown&#xff08;改变文件的所属主和所属组&#xff09;、truncate&#xff08;截…

stat函数(stat、fstat、lstat)

#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> //需包含头文件 有如下三个函数的函数原型&#xff1a; int stat(const char *path, struct stat *buf); 第一个形参&#xff1a;指出文件&#xff08;文件路径&#xff09;&…

1062. Talent and Virtue (25)

About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about peoples talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a "sage&#xff08;圣人&#xff09;"…

access、strtol函数的使用(后者为C库函数)

#include <unistd.h> int access(const char *pathname, int mode); 作用&#xff1a;检查调用该函数的进程是否可以对指定的文件执行某种操作。 第一个形参&#xff1a;文件名&#xff1b;第二个形参&#xff1a;R_OK&#xff08;是否可读&#xff09;、W_OK&#xf…

chmod、chown函数的使用

#include <sys/stat.h> int chmod(const char *path, mode_t mode); int fchmod(int fd, mode_t mode); 作用&#xff1a;改变指定文件的权限。第二个参数&#xff1a;mode必须为一个8进制数&#xff1b;返回值为0表示成功&#xff0c;-1表示失败。 //代码 #include…

606. 根据二叉树创建字符串

你需要采用前序遍历的方式&#xff0c;将一个二叉树转换成一个由括号和整数组成的字符串。 空节点则用一对空括号 "()" 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。 示例 1: 输入: 二叉树: [1,2,3,4] 1 / \ …

truncate、rename函数的使用

#include <unistd.h> #include <sys/types.h> int truncate(const char *path, off_t length); int ftruncate(int fd, off_t length); 作用&#xff1a;用于拓展或截断文件。将参数path 指定的文件大小改为参数length 指定的大小。如果原来的文件大小比参数le…

【Leetcode】112. 路径总和

给定一个二叉树和一个目标和&#xff0c;判断该树中是否存在根节点到叶子节点的路径&#xff0c;这条路径上所有节点值相加等于目标和。 说明: 叶子节点是指没有子节点的节点。 示例: 给定如下二叉树&#xff0c;以及目标和 sum 22&#xff0c; 5 / \ …

link、symlink、readlink、unlink函数的使用

#include <unistd.h> int link(const char *oldpath, const char *newpath); 作用&#xff1a;创建一个硬链接 0成功 -1 失败 //代码 #include <stdio.h> #include <stdlib.h> #include <unistd.h>int main(int argc, char* argv[]) {if(ar…

【Leetcode】113. 路径总和 II

给定一个二叉树和一个目标和&#xff0c;找到所有从根节点到叶子节点路径总和等于给定目标和的路径。 说明: 叶子节点是指没有子节点的节点。 示例: 给定如下二叉树&#xff0c;以及目标和 sum 22&#xff0c; 5 / \ 4 8 / / \ …

目录操作相关的系统函数

主要介绍几个常用函数的使用方法&#xff1a;chdir&#xff08;改变进程的当前工作目录&#xff09;、getcwd&#xff08;获取当前进程的工作目录&#xff09;、mkdir&#xff08;创建目录&#xff09;、rmdir&#xff08;删除空目录&#xff09;、opendir&#xff08;打开一个…

1079. Total Sales of Supply Chain (25)

A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;经销商&#xff09;, and suppliers&#xff08;供应商&#xff09;-- everyone involved in moving a product from supplier to customer. Starting from one root suppli…

chdir、getcwd、mkdir、rmdir函数

#include <unistd.h> int chdir(const char *path); int fchdir(int fd); 作用&#xff1a;改变调用这一函数的进程&#xff08;即程序执行&#xff09;的当前工作目录&#xff0c;注意不是shell的当前工作目录。 返回值&#xff1a;0成功 -1失败 #include <unis…

【Leetcode | 235】 235. 二叉搜索树的最近公共祖先

给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为&#xff1a;“对于有根树 T 的两个结点 p、q&#xff0c;最近公共祖先表示为一个结点 x&#xff0c;满足 x 是 p、q 的祖先且 x 的深度尽可能大&#xff08;一个节点也可以是它自己…

1090. Highest Price in Supply Chain (25)

A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;经销商&#xff09;, and suppliers&#xff08;供应商&#xff09;-- everyone involved in moving a product from supplier to customer. Starting from one root suppli…

opendir、readdir和closedir函数

注意&#xff1a;在Linux中&#xff0c;目录的输入格式&#xff1a;/mnt//fghs、/mnt/fghs、/mnt/fghs和/mnt/fghs//是等效的&#xff0c;都一样。 #include <sys/types.h> #include <dirent.h> DIR *opendir(const char *name); DIR *fdopendir(int fd); 返回…

146. LRU缓存机制

运用你所掌握的数据结构&#xff0c;设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作&#xff1a; 获取数据 get 和 写入数据 put 。 获取数据 get(key) - 如果密钥 (key) 存在于缓存中&#xff0c;则获取密钥的值&#xff08;总是正数&#xff09;&#xff…