.\" 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