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.pswp.cn/news/385404.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385404.shtml
英文地址,請注明出處:http://en.pswp.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…