How to Know When to Stop Reading a File C++
IEEE Std 1003.one, 2004 Edition
Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.
NAME
pread, read - read from a file
SYNOPSIS
#include <unistd.h>
[XSI] ssize_t pread(int fildes , void * buf , size_t nbyte , off_t commencement );
ssize_t read(int fildes , void * buf , size_t nbyte );
Description
The read() function shall attempt to read nbyte bytes from the file associated with the open file descriptor, fildes, into the buffer pointed to by buf. The behavior of multiple concurrent reads on the same pipe, FIFO, or terminal device is unspecified.
Before any action described beneath is taken, and if nbyte is zero, the read() function may notice and return errors as described below. In the absence of errors, or if error detection is not performed, the read() office shall render zero and accept no other results.
On files that support seeking (for example, a regular file), the read() shall outset at a position in the file given by the file start associated with fildes. The file beginning shall be incremented by the number of bytes actually read.
Files that do not back up seeking-for instance, terminals-e'er read from the current position. The value of a file commencement associated with such a file is undefined.
No data transfer shall occur by the current stop-of-file. If the starting position is at or subsequently the end-of-file, 0 shall be returned. If the file refers to a device special file, the result of subsequent read() requests is implementation-divers.
If the value of nbyte is greater than {SSIZE_MAX}, the result is implementation-defined.
When attempting to read from an empty piping or FIFO:
If no process has the pipe open for writing, read() shall render 0 to signal finish-of-file.
If some process has the pipe open for writing and O_NONBLOCK is set, read() shall render -1 and set errno to [EAGAIN].
If some process has the piping open for writing and O_NONBLOCK is clear, read() shall block the calling thread until some data is written or the pipe is closed past all processes that had the piping open for writing.
When attempting to read a file (other than a piping or FIFO) that supports non-blocking reads and has no data currently available:
If O_NONBLOCK is set up, read() shall render -1 and ready errno to [EAGAIN].
If O_NONBLOCK is clear, read() shall block the calling thread until some data becomes available.
The utilize of the O_NONBLOCK flag has no result if there is some data available.
The read() function reads data previously written to a file. If any portion of a regular file prior to the cease-of-file has not been written, read() shall return bytes with value 0. For instance, lseek() allows the file offset to be gear up beyond the end of existing data in the file. If data is later written at this point, subsequent reads in the gap between the previous end of data and the newly written data shall render bytes with value 0 until information is written into the gap.
Upon successful completion, where nbyte is greater than 0, read() shall mark for update the st_atime field of the file, and shall render the number of bytes read. This number shall never be greater than nbyte. The value returned may be less than nbyte if the number of bytes left in the file is less than nbyte, if the read() request was interrupted by a bespeak, or if the file is a pipe or FIFO or special file and has fewer than nbyte bytes immediately bachelor for reading. For instance, a read() from a file associated with a terminal may return i typed line of data.
If a read() is interrupted by a indicate before it reads any data, it shall return -1 with errno set to [EINTR].
If a read() is interrupted past a indicate subsequently it has successfully read some data, it shall render the number of bytes read.
For regular files, no data transfer shall occur past the get-go maximum established in the open file clarification associated with fildes.
If fildes refers to a socket, read() shall be equivalent to recv() with no flags ready.
[SIO] If the O_DSYNC and O_RSYNC $.25 have been set, read I/O operations on the file descriptor shall complete as defined past synchronized I/O data integrity completion. If the O_SYNC and O_RSYNC bits have been set, read I/O operations on the file descriptor shall complete as defined by synchronized I/O file integrity completion.
[SHM] If fildes refers to a shared memory object, the result of the read() part is unspecified.
[TYM] If fildes refers to a typed retentivity object, the result of the read() function is unspecified.
[XSR] A read() from a STREAMS file tin can read data in iii different modes: byte-stream mode, message-nondiscard mode, and message-discard manner. The default shall be byte-stream style. This can be changed using the I_SRDOPT ioctl() request, and can be tested with I_GRDOPT ioctl(). In byte-stream mode, read() shall retrieve data from the STREAM until as many bytes as were requested are transferred, or until at that place is no more information to be retrieved. Byte-stream fashion ignores message boundaries.
In STREAMS message-nondiscard manner, read() shall recall information 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 information shall be left on the STREAM, and can be retrieved by the adjacent read() phone call. Message-discard mode likewise retrieves data until as many bytes as were requested are transferred, or a message purlieus is reached. Even so, unread data remaining in a message after the read() returns shall exist discarded, and shall not be bachelor for a subsequent read(), getmsg(), or getpmsg() call.
How read() handles zero-byte STREAMS letters is adamant by the current read mode setting. In byte-stream manner, read() shall have information until it has read nbyte bytes, or until there is no more than data to read, or until a nothing-byte message block is encountered. The read() function shall then render the number of bytes read, and identify the zero-byte message back on the STREAM to be retrieved by the next read(), getmsg(), or getpmsg(). In bulletin-nondiscard way or message-discard mode, a zero-byte message shall render 0 and the message shall be removed from the STREAM. When a zero-byte message is read as the commencement message on a STREAM, the message shall be removed from the STREAM and 0 shall be returned, regardless of the read mode.
A read() from a STREAMS file shall return 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 tin can only procedure messages that contain a data part merely do not contain a control part. The read() shall neglect if a message containing a control office is encountered at the STREAM head. This default action tin be inverse by placing the STREAM in either control-data mode or control-discard fashion with the I_SRDOPT ioctl() command. In control-data mode, read() shall catechumen any control function to data and pass it to the awarding before passing any data part originally present in the same message. In control-discard mode, read() shall discard message control parts but return to the procedure whatsoever data part in the message.
In addition, read() shall neglect if the STREAM head had processed an asynchronous mistake before the call. In this case, the value of errno shall not reflect the event of read(), but reflect the prior error. If a hangup occurs on the STREAM beingness read, read() shall continue to operate normally until the STREAM head read queue is empty. Thereafter, information technology shall return 0.
[XSI] The pread() function shall be equivalent to read(), except that information technology shall read from a given position in the file without changing the file pointer. The first three arguments to pread() are the same equally read() with the addition of a fourth argument start for the desired position within the file. An endeavor to perform a pread() on a file that is incapable of seeking shall consequence in an mistake.
RETURN VALUE
Upon successful completion, read() [XSI] and pread() shall render a non-negative integer indicating the number of bytes actually read. Otherwise, the functions shall render -1 and set errno to bespeak the error.
ERRORS
The read() and [XSI] pread() functions shall fail if:
- [EAGAIN]
- The O_NONBLOCK flag is set for the file descriptor and the thread would be delayed.
- [EBADF]
- The fildes argument is not a valid file descriptor open for reading.
- [EBADMSG]
- [XSR] The file is a STREAM file that is ready to control-normal way and the bulletin waiting to exist read includes a control part.
- [EINTR]
- The read operation was terminated due to the receipt of a betoken, and no information was transferred.
- [EINVAL]
- [XSR] The STREAM or multiplexer referenced by fildes is linked (directly or indirectly) downstream from a multiplexer.
- [EIO]
- The process is a member of a background process attempting to read from its decision-making terminal, the process is ignoring or blocking the SIGTTIN signal, or the procedure group is orphaned. This error may too be generated for implementation-defined reasons.
- [EISDIR]
- [XSI] The fildes statement refers to a directory and the implementation does not allow the directory to exist read using read() or pread(). The readdir() office should exist used instead.
- [EOVERFLOW]
- The file is a regular file, nbyte is greater than 0, the starting position is before the cease-of-file, and the starting position is greater than or equal to the get-go maximum established in the open file description associated with fildes.
The read() function shall fail if:
- [EAGAIN] or [EWOULDBLOCK]
- The file descriptor is for a socket, is marked O_NONBLOCK, and no data is waiting to be received.
- [ECONNRESET]
- A read was attempted on a socket and the connection was forcibly closed by its peer.
- [ENOTCONN]
- A read was attempted on a socket that is not connected.
- [ETIMEDOUT]
- A read was attempted on a socket and a transmission timeout occurred.
The read() and [XSI] pread() functions may fail if:
- [EIO]
- A physical I/O fault has occurred.
- [ENOBUFS]
- Insufficient resource were available in the system to perform the operation.
- [ENOMEM]
- Bereft retention was bachelor to fulfill the request.
- [ENXIO]
- A request was made of a nonexistent device, or the request was outside the capabilities of the device.
The pread() office shall fail, and the file pointer shall remain unchanged, if:
- [EINVAL]
- [XSI] The offset argument is invalid. The value is negative.
- [EOVERFLOW]
- [XSI] The file is a regular file and an endeavour was made to read at or across the offset maximum associated with the file.
- [ENXIO]
- [XSI] A request was exterior the capabilities of the device.
- [ESPIPE]
- [XSI] fildes is associated with a piping or FIFO.
The following sections are informative.
EXAMPLES
Reading Information into a Buffer
The following example reads data from the file associated with the file descriptor fd into the buffer pointed to past buf.
#include <sys/types.h> #include <unistd.h> ... char buf[twenty]; size_t nbytes; ssize_t bytes_read; int fd; ... nbytes = sizeof(buf); bytes_read = read(fd, buf, nbytes); ...
Application USAGE
None.
RATIONALE
This volume of IEEE Std 1003.i-2001 does not specify the value of the file offset after an mistake is returned; there are too many cases. For programming errors, such as [EBADF], the concept is meaningless since no file is involved. For errors that are detected immediately, such as [EAGAIN], clearly the pointer should not change. After an interrupt or hardware error, nevertheless, an updated value would be very useful and is the beliefs of many implementations.
Note that a read() of zero bytes does not change st_atime. A read() that requests more zero bytes, but returns cipher, shall alter st_atime.
Implementations are allowed, but not required, to perform fault checking for read() requests of aught bytes.
Input and Output
The use of I/O with large byte counts has always presented problems. Ideas such every bit lread() and lwrite() (using and returning longdue south) were considered at one time. The current solution is to use abstract types on the ISO C standard function to read() and write(). The abstract types can be declared and then that existing functions piece of work, simply can also be declared then that larger types tin be represented in future implementations. It is presumed that whatever constraints limit the maximum range of size_t also limit portable I/O requests to the same range. This volume of IEEE Std 1003.1-2001 also limits the range further by requiring that the byte count be express so that a signed return value remains meaningful. Since the render blazon is also a (signed) abstract type, the byte count can be divers by the implementation to be larger than an int tin can hold.
The standard developers considered adding atomicity requirements to a piping or FIFO, just recognized that due to the nature of pipes and FIFOs there could be no guarantee of atomicity of reads of {PIPE_BUF} or any other size that would exist an help to applications portability.
This volume of IEEE Std 1003.1-2001 requires that no action be taken for read() or write() when nbyte is nada. This is non intended to take precedence over detection of errors (such as invalid buffer pointers or file descriptors). This is consistent with the rest of this book of IEEE Std 1003.ane-2001, but the phrasing here could be misread to require detection of the zero case before whatsoever other errors. A value of naught is to exist considered a correct value, for which the semantics are a no-op.
I/O is intended to be atomic to ordinary files and pipes and FIFOs. Diminutive means that all the bytes from a single operation that started out together end upwards together, without interleaving from other I/O operations. It is a known attribute of terminals that this is not honored, and terminals are explicitly (and implicitly permanently) excepted, making the behavior unspecified. The behavior for other device types is also left unspecified, merely the wording is intended to imply that futurity standards might choose to specify atomicity (or not).
In that location were recommendations to add format parameters to read() and write() in club to handle networked transfers among heterogeneous file system and base hardware types. Such a facility may exist required for support by the OSI presentation of layer services. Yet, information technology was determined that this should correspond with similar C-language facilities, and that is beyond the scope of this volume of IEEE Std 1003.ane-2001. The concept was suggested to the developers of the ISO C standard for their consideration every bit a possible area for futurity work.
In iv.3 BSD, a read() or write() that is interrupted past a signal before transferring whatever data does not by default return an [EINTR] error, but is restarted. In 4.ii BSD, four.iii BSD, and the Eighth Edition, in that location is an additional role, select(), whose purpose is to interruption until specified action (data to read, space to write, so on) is detected on specified file descriptors. It is common in applications written for those systems for select() to be used before read() in situations (such as keyboard input) where interruption of I/O due to a signal is desired.
The issue of which files or file types are interruptible is considered an implementation design result. This is often affected primarily past hardware and reliability issues.
There are no references to actions taken following an "unrecoverable error". It is considered across the scope of this volume of IEEE Std 1003.1-2001 to describe what happens in the case of hardware errors.
Previous versions of IEEE Std 1003.1-2001 allowed two very different behaviors with regard to the handling of interrupts. In order to minimize the resulting defoliation, it was decided that IEEE Std 1003.one-2001 should support only one of these behaviors. Historical practice on AT&T-derived systems was to have read() and write() return -ane and gear up errno to [EINTR] when interrupted after some, but not all, of the data requested had been transferred. Withal, the U.Due south. Department of Commerce FIPS 151-1 and FIPS 151-2 require the historical BSD beliefs, in which read() and write() return the number of bytes actually transferred before the interrupt. If -1 is returned when any data is transferred, information technology is difficult to recover from the mistake on a seekable device and impossible on a non-seekable device. Most new implementations support this behavior. The behavior required by IEEE Std 1003.ane-2001 is to render the number of bytes transferred.
IEEE Std 1003.1-2001 does not specify when an implementation that buffers read()s actually moves the data into the user-supplied buffer, so an implementation may choose to do this at the latest possible moment. Therefore, an interrupt arriving earlier may non cause read() to return a partial byte count, but rather to return -1 and ready errno to [EINTR].
Consideration was too given to combining the two previous options, and setting errno to [EINTR] while returning a short count. Nevertheless, not only is there no existing practise that implements this, it is also contradictory to the idea that when errno is set, the part responsible shall return -1.
Futurity DIRECTIONS
None.
Come across Besides
fcntl(), ioctl(), lseek(), open(), pipe(), readv(), the Base of operations Definitions book of IEEE Std 1003.1-2001, Chapter xi, General Terminal Interface, <stropts.h>, <sys/uio.h>, <unistd.h>
CHANGE HISTORY
Showtime released in Effect one. Derived from Issue 1 of the SVID.
Issue 5
The Description is updated for alignment with the POSIX Realtime Extension and the POSIX Threads Extension.
Big File Summit extensions are added.
The pread() role is added.
Issue half dozen
The DESCRIPTION and ERRORS sections are updated then that references to STREAMS are marked every bit part of the XSI STREAMS Choice Group.
The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:
The DESCRIPTION now states that if read() is interrupted by a indicate after it has successfully read some data, information technology returns the number of bytes read. In Outcome three, it was optional whether read() returned the number of bytes read, or whether it returned -1 with errno set to [EINTR]. This is a FIPS requirement.
In the Clarification, text is added to indicate that for regular files, no information transfer occurs past the showtime maximum established in the open file clarification associated with fildes. This change is to support large files.
The [EOVERFLOW] mandatory error condition is added.
The [ENXIO] optional fault status is added.
Text referring to sockets is added to the Clarification.
The post-obit changes were made to marshal with the IEEE P1003.1a draft standard:
The effect of reading null bytes is antiseptic.
The DESCRIPTION is updated for alignment with IEEE Std 1003.1j-2000 by specifying that read() results are unspecified for typed memory objects.
New RATIONALE is added to explain the atomicity requirements for input and output operations.
The following mistake atmospheric condition are added for operations on sockets: [EAGAIN], [ECONNRESET], [ENOTCONN], and [ETIMEDOUT].
The [EIO] error is made optional.
The following mistake weather condition are added for operations on sockets: [ENOBUFS] and [ENOMEM].
The readv() role is split out into a separate reference page.
IEEE Std 1003.ane-2001/Cor two-2004, item XSH/TC2/D6/108 is applied, updating the [EAGAIN] fault in the ERRORS section from "the procedure would be delayed" to "the thread would be delayed".
IEEE Std 1003.i-2001/Cor 2-2004, item XSH/TC2/D6/109 is practical, making an editorial correction in the RATIONALE section.
Stop of informative text.
POSIX ® is a registered Trademark of The IEEE.
[ Main Alphabetize | XBD | XCU | XSH | XRAT ]
Source: https://pubs.opengroup.org/onlinepubs/009604599/functions/read.html
0 Response to "How to Know When to Stop Reading a File C++"
Post a Comment