Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define_POSIX_SOURCE
#include <unistd.h>
ssize_t read(int fs, void *buf, size_t N);
#define _XOPEN_SOURCE_EXTENDED 1
#include <unistd.h>
ssize_t read(int fs, void *buf, ssize_t N);
#define _OE_SOCKETS
#include <unistd.h>
ssize_t read(int socket, void *buf, ssize_t N);
From the file indicated by the file descriptor fs, the read() function reads N bytes of input into the memory area indicated by buf. A successful read() updates the access time for the file.
If fs refers to a regular file or any other type of file on which the process can seek, read() begins reading at the file offset associated with fs. If successful, read() changes the file offset by the number of bytes read. N should not be greater than INT_MAX (defined in the limits.h header file).
If fs refers to a file on which the process cannot seek, read() begins reading at the current position. There is no file offset associated with such a file.
Behavior for sockets: The read() call reads data on a socket with descriptor fs and stores it in a buffer. The read() all applies only to connected sockets. This call returns up to N bytes of data. If there are fewer bytes available than requested, the call returns the number currently available. If data is not available for the socket fs, and the socket is in blocking mode, the read() call blocks the caller until data arrives. If data is not available, and the socket is in nonblocking mode, read() returns a -1 and sets the error code to EWOULDBLOCK. See ioctl() — Control device or fcntl() — Control open file descriptors for a description of how to set nonblocking mode.
For datagram sockets, this call returns the entire datagram that was sent, provided that the datagram fits into the specified buffer. Excess datagram data is discarded. Stream sockets act like streams of information with no boundaries separating data. For example, if applications A and B are connected with a stream socket and application A sends 1000 bytes, each call to this function can return 1 byte, or 10 bytes, or the entire 1000 bytes. Therefore, applications using stream sockets should place this call in a loop, calling this function until all data has been received.
Behavior for streams: A read() from a STREAMS file can read data in three different modes: byte-stream mode, message-nondiscard mode, and message-discard mode. The default is byte-stream mode. This can be changed using the I_SRDOPT ioctl() request, and can be tested with the I_GRDOPT ioctl(). In byte-stream mode, read() retrieves data from the STREAM until as many bytes as were requested are transferred, or until there is no more data to be retrieved. Byte-stream mode ignores message boundaries.
In STREAMS message-nondiscard mode, read() retrieves data until as many bytes as were requested are transferred, or until a message boundary is reached. If read() does not retrieve all the data in a message, the remaining data is left on the STREAM, and can be retrieved by the next read() call. Message-discard mode also retrieves data until as many bytes as were requested are transferred, or a message boundary is reached. However, unread data remaining in a message after the read() returns is discarded, and is not available for a subsequent read(), readv() or getmsg() call.
How read() handles zero-byte STREAMS messages is determined by the current read mode setting. In byte-stream mode, read() accepts data until it has read N bytes, or until there is no more data to read, or until a zero-byte message block is encountered. The read() function then returns the number of bytes read, and places the zero-byte message back on the STREAM to be retrieved by the next read(), readv() or getmsg(). In message-nondiscard mode or message-discard mode, a zero-byte message returns 0 and the message is removed from the STREAM. When a zero-byte message is read as the first message on a STREAM, the message is removed from the STREAM and 0 is returned, regardless of the read mode.
A read() from a STREAMS file returns the data in the message at the front of the STREAM head read queue, regardless of the priority band of the message.
By default, STREAMs are in control-normal mode, in which a read() from a STREAMS file can only process messages that contain a data part but do not contain a control part. The read() fails if a message containing a control part is encountered at the STREAM head. This default action can be changed by placing the STREAM in either control-data mode or control-discard mode with the I_SRDOPT ioctl() command. In control-data mode, read() converts any control part to data and passes it to the application before passing any data part originally present in the same message. In control-discard mode, read() discards message control parts but returns to the process any data part in the message.
In addition, read() and readv() will fail if the STREAM head had processed an asynchronous error before the call. In this case, the value of errno does not reflect the result of read() or readv() but reflects the prior error. If a hang-up occurs on the STREAM being read, read() continues to operate normally until the STREAM head read queue is empty. Thereafter, it returns 0.
Large file support for z/OS UNIX files: Large z/OS UNIX files are supported automatically for AMODE 64 C/C++ applications. AMODE 31 C/C++ applications must be compiled with the option LANGLVL(LONGLONG) and define the _LARGE_FILES feature test macro before any headers are included to enable this function to operate on z/OS UNIX files that are larger than 2 GB in size. File size and offset fields are enlarged to 63 bits in width. Therefore, any other function operating on the file is required to define the _LARGE_FILES feature test macro as well.
If the starting position for the read operation is at the end of the file or beyond, read() returns 0.
If read() is reading a regular file and encounters a part of the file that has not been written (but before the end of the file), read() places 0 bytes into buf in place of the unwritten bytes.
If the number of bytes of input that you want to read is 0, read() simply returns 0 without attempting any other action.
If the connection is broken on a stream socket, but data is available, then the read() function reads the data and gives no error. If the connection is broken on a stream socket, but no data is available, then the read() function returns 0 bytes as EOF.
⁄* CELEBR03
This example opens a file and reads input.
*⁄
#define _POSIX_SOURCE
#include <fcntl.h>
#include <unistd.h>
#undef _POSIX_SOURCE
#include <stdio.h>
main() {
int ret, fd;
char buf[1024];
system("ls -l ⁄ >| ls.output");
if ((fd = open("ls.output", O_RDONLY)) < 0)
perror("open() error");
else {
while ((ret = read(fd, buf, sizeof(buf)-1)) > 0) {
buf[ret] = 0x00;
printf("block read: \n<%s>\n", buf);
}
close(fd);
}
unlink("ls.output");
}
block read:
<total 0
drwxr-xr-x 3 USER1 SYS1 0 Apr 16 07:59 bin
drwxr-xr-x 2 USER1 SYS1 0 Apr 6 10:20 dev
drwxr-xr-x 4 USER1 SYS1 0 Apr 16 07:59 etc
drwxr-xr-x 2 USER1 SYS1 0 Apr 6 10:15 lib
drwxrwxrwx 2 USER1 SYS1 0 Apr 16 07:55 tmp
drwxr-xr-x 2 USER1 SYS1 0 Apr 6 10:15 u
drwxr-xr-x 6 USER1 SYS1 0 Apr 6 10:15 usr
>