.\" Hey Emacs! This file is -*- nroff -*- source. .\" .\" This manpage is Copyright (C) 1992 Drew Eckhardt; .\" 1993 Michael Haardt, Ian Jackson. .\" .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" .\" Modified Sat Jul 24 00:06:00 1993 by Rik Faith .\" Modified Wed Jan 17 16:02:32 1996 by Michael Haardt .\" .\" Modified Thu Apr 11 19:26:35 1996 by Andries Brouwer .\" Modified Sun Jul 21 18:59:33 1996 by Andries Brouwer .\" Modified Fri Jan 31 16:47:33 1997 by Eric S. Raymond .\" Modified Sat Jul 12 20:45:39 1997 by Michael Haardt .\" .\" 中文版版權所有 byeyear AND www.linuxforum.net 2002 .\" .TH READ 2 "July 12, 1997" "Linux 2.0.32" "Linux Programmer's Manual" .SH NAME read \- 在文件描述符上執行讀操作 .SH 概述 .nf .B #include .sp .BI "ssize_t read(int " fd ", void *" buf ", size_t " count ); .fi .SH 描述 .B read() 從文件描述符 .I fd 中讀取 .I count 字節的數據並放入從 .IR buf 開始的緩衝區中. .PP 如果 .I count 爲零,\fBread()\fP返回0,不執行其他任何操作. 如果 .I count 大於SSIZE_MAX,那麼結果將不可預料. .PP .SH "返回值" 成功時返回讀取到的字節數(爲零表示讀到文件描述符), 此返回值受文件剩餘字節數限制.當返回值小於指定的字節數時 並不意味着錯誤;這可能是因爲當前可讀取的字節數小於指定的 字節數(比如已經接近文件結尾,或者正在從管道或者終端讀取數 據,或者 \fBread()\fP被信號中斷). 發生錯誤時返回\-1,並置 .I errno 爲相應值.在這種情況下無法得知文件偏移位置是否有變化. .SH 錯誤代碼 .TP .B EINTR 在讀取到數據以前調用被信號所中斷. .TP .B EAGAIN 使用 .B O_NONBLOCK 標誌指定了非阻塞式輸入輸出,但當前沒有數據可讀. .TP .B EIO 輸入輸出錯誤.可能是正處於後臺進程組進程試圖讀取其 控制終端,但讀操作無效,或者被信號SIGTTIN所阻塞, 或者其進程組是孤兒進程組.也可能執行的是讀磁盤或者 磁帶機這樣的底層輸入輸出錯誤. .TP .B EISDIR .I fd 指向一個目錄. .TP .B EBADF .I fd 不是一個合法的文件描述符,或者不是爲讀操作而打開. .TP .B EINVAL .I fd 所連接的對象不可讀. .TP .B EFAULT .I buf 超出用戶可訪問的地址空間. .PP 也可能發生其他錯誤,具體情況和 .IR fd 所連接的對象有關. POSIX 允許 .B read 在讀取了一定量的數據後被信號所中斷,並返回 \-1(且 .I errno 被設置爲EINTR),或者返回已讀取的數據量. .SH 兼容於 SVr4, SVID, AT&T, POSIX, X/OPEN, BSD 4.3 .SH 限制 在NFS文件系統中,讀取小塊數據僅更新時間標記,之後的調用 不再讀取服務器端的數據.這是因爲客戶端把數據放在緩存裏. 由於大多數情況下不存在NFS服務器向客戶端的讀操作, 所以NFS客戶必須將更新時間標記的操作放在服務器端,而 數據可以放在客戶端的緩存裏留待以後更新.UNIX也可以禁用 客戶端的緩存,但那樣的話大多數情況下會導致服務器性能下降. .SH 參見 .BR close (2), .BR fcntl (2), .BR ioctl (2), .BR lseek (2), .BR readdir (2), .BR readlink (2), .BR select (2), .BR write (2), .BR fread (3) .SH "[中文版維護人]" .B byeyear .SH "[中文版最新更新]" .B 2002.02.02 .SH "《中國linux論壇man手冊頁翻譯計劃》:" .BI http://cmpp.linuxforum.net .SH "跋" .br 本頁面中文版由中文 man 手冊頁計劃提供。 .br 中文 man 手冊頁計劃:\fBhttps://github.com/man-pages-zh/manpages-zh\fR