How to Read Between Two Bytes Offset File C++
IEEE Std 1003.one-2017 (Revision of IEEE Std 1003.1-2008)
Copyright © 2001-2018 IEEE and The Open Group
NAME
pread, read - read from a file
SYNOPSIS
#include <unistd.h>
ssize_t pread(int
fildes , void * buf , size_t nbyte , off_t commencement );
ssize_t read(int fildes , void * buf , size_t nbyte );
Clarification
The read() function shall attempt to read nbyte bytes from the file associated with the open up file descriptor, fildes, into the buffer pointed to by buf. The behavior of multiple concurrent reads on the same pipe, FIFO, or last device is unspecified.
Before any activeness described below is taken, and if nbyte is zero, the read() function may notice and render errors equally described below. In the absence of errors, or if error detection is not performed, the read() function shall render zip and have no other results.
On files that support seeking (for example, a regular file), the read() shall start at a position in the file given by the file kickoff associated with fildes. The file offset shall exist incremented by the number of bytes actually read.
Files that do not support seeking-for case, terminals-ever read from the current position. The value of a file offset associated with such a file is undefined.
No data transfer shall occur by the electric current end-of-file. If the starting position is at or after the end-of-file, 0 shall exist returned. If the file refers to a device special file, the result of subsequent read() requests is implementation-defined.
If the value of nbyte is greater than {SSIZE_MAX}, the result is implementation-defined.
When attempting to read from an empty pipe or FIFO:
If no process has the pipe open for writing, read() shall render 0 to betoken end-of-file.
If some process has the piping open for writing and O_NONBLOCK is set up, read() shall return -one and set errno to [EAGAIN].
If some process has the pipe open for writing and O_NONBLOCK is clear, read() shall cake the calling thread until some information is written or the pipage is airtight by all processes that had the pipage open for writing.
When attempting to read a file (other than a pipe or FIFO) that supports not-blocking reads and has no data currently available:
If O_NONBLOCK is gear up, read() shall return -1 and set errno to [EAGAIN].
If O_NONBLOCK is clear, read() shall block the calling thread until some data becomes bachelor.
The use of the O_NONBLOCK flag has no effect if there is some data available.
The read() part reads data previously written to a file. If any portion of a regular file prior to the end-of-file has not been written, read() shall render bytes with value 0. For instance, lseek() allows the file offset to be fix beyond the terminate of existing information in the file. If data is later written at this point, subsequent reads in the gap between the previous terminate of data and the newly written data shall return 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 terminal data access timestamp of the file, and shall return 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 available for reading. For example, a read() from a file associated with a terminal may return one typed line of data.
If a read() is interrupted past a point before it reads any data, information technology shall render -i with errno gear up to [EINTR].
If a read() is interrupted past a signal after information technology has successfully read some data, information technology shall render the number of bytes read.
For regular files, no data transfer shall occur past the offset maximum established in the open file description associated with fildes.
If fildes refers to a socket, read() shall be equivalent to recv() with no flags set.
[SIO] If the O_DSYNC and O_RSYNC bits have been set, read I/O operations on the file descriptor shall complete as defined by synchronized I/O data integrity completion. If the O_SYNC and O_RSYNC $.25 have been set, read I/O operations on the file descriptor shall complete as divers by synchronized I/O file integrity completion.
[SHM] If fildes refers to a shared retentivity object, the effect of the read() office is unspecified.
[TYM] If fildes refers to a typed memory object, the effect of the read() function is unspecified.
[OB XSR] A read() from a STREAMS file can read information in three different modes: byte-stream mode, message-nondiscard mode, and message-discard way. The default shall exist 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 think 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 fashion ignores message boundaries.
In STREAMS bulletin-nondiscard way, read() shall retrieve information until as many bytes as were requested are transferred, or until a message purlieus is reached. If read() does non remember all the information in a bulletin, the remaining information shall exist left on the STREAM, and can exist retrieved by the next read() call. Message-discard mode also retrieves information until as many bytes as were requested are transferred, or a bulletin boundary is reached. However, unread information remaining in a message afterwards the read() returns shall be discarded, and shall not be bachelor for a subsequent read(), getmsg(), or getpmsg() call.
How read() handles zero-byte STREAMS messages is determined by the current read fashion setting. In byte-stream mode, read() shall have information until it has read nbyte bytes, or until there is no more data to read, or until a nil-byte message block is encountered. The read() role shall then render the number of bytes read, and place the zero-byte message dorsum on the STREAM to be retrieved by the adjacent read(), getmsg(), or getpmsg(). In bulletin-nondiscard manner or message-discard mode, a zero-byte message shall render 0 and the message shall exist removed from the STREAM. When a cipher-byte message is read every bit the kickoff 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 bulletin.
By default, STREAMs are in control-normal style, in which a read() from a STREAMS file can only procedure messages that contain a information part but do non contain a control function. The read() shall fail if a bulletin containing a control part is encountered at the STREAM caput. This default activeness tin can exist inverse by placing the STREAM in either control-information mode or control-discard manner with the I_SRDOPT ioctl() command. In control-data mode, read() shall convert any control part to data and pass it to the application before passing any data office originally present in the same message. In command-discard mode, read() shall discard message command parts only return to the procedure any data part in the message.
In improver, read() shall neglect if the STREAM caput had processed an asynchronous error before the call. In this case, the value of errno shall non reflect the issue of read(), but reflect the prior error. If a hangup occurs on the STREAM being read, read() shall keep to operate ordinarily until the STREAM caput read queue is empty. Thereafter, it shall render 0.
The pread() function shall be equivalent to read(), except that it shall read from a given position in the file without irresolute the file kickoff. The first three arguments to pread() are the aforementioned as read() with the addition of a fourth statement offset for the desired position inside the file. An attempt to perform a pread() on a file that is incapable of seeking shall issue in an error.
Return VALUE
Upon successful completion, these functions shall return a not-negative integer indicating the number of bytes really read. Otherwise, the functions shall render -ane and set up errno to betoken the error.
ERRORS
These functions shall neglect if:
- [EAGAIN]
- The file is neither a pipe, nor a FIFO, nor a socket, the O_NONBLOCK flag is prepare for the file descriptor, and the thread would be delayed in the read performance.
- [EBADF]
- The fildes argument is not a valid file descriptor open for reading.
- [EBADMSG]
- [OB XSR] The file is a STREAM file that is set to control-normal mode and the bulletin waiting to be read includes a control office.
- [EINTR]
- The read operation was terminated due to the receipt of a signal, and no information was transferred.
- [EINVAL]
- [OB XSR] The STREAM or multiplexer referenced by fildes is linked (direct or indirectly) downstream from a multiplexer.
- [EIO]
- The process is a member of a background process group attempting to read from its decision-making concluding, and either the calling thread is blocking SIGTTIN or the process is ignoring SIGTTIN or the process group of the procedure is orphaned. This error may also be generated for implementation-defined reasons.
- [EISDIR]
- [XSI] The fildes argument refers to a directory and the implementation does not let the directory to be read using read() or pread(). The readdir() role should be used instead.
- [EOVERFLOW]
- The file is a regular file, nbyte is greater than 0, the starting position is before the end-of-file, and the starting position is greater than or equal to the showtime maximum established in the open file description associated with fildes.
The pread() office shall fail if:
- [EINVAL]
- The file is a regular file or block special file, and the offset argument is negative. The file offset shall remain unchanged.
- [ESPIPE]
- The file is incapable of seeking.
The read() part shall fail if:
- [EAGAIN]
- The file is a pipe or FIFO, the O_NONBLOCK flag is set for the file descriptor, and the thread would be delayed in the read operation.
- [EAGAIN] or [EWOULDBLOCK]
- The file is a socket, the O_NONBLOCK flag is fix for the file descriptor, and the thread would exist delayed in the read functioning.
- [ECONNRESET]
- A read was attempted on a socket and the connection was forcibly airtight past 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.
These functions may fail if:
- [EIO]
- A physical I/O error has occurred.
- [ENOBUFS]
- Insufficient resources were available in the system to perform the operation.
- [ENOMEM]
- Bereft memory was available to fulfill the request.
- [ENXIO]
- A request was made of a nonexistent device, or the request was exterior the capabilities of the device.
The following sections are informative.
EXAMPLES
Reading Data into a Buffer
The post-obit case reads data from the file associated with the file descriptor fd into the buffer pointed to by buf.
#include <sys/types.h> #include <unistd.h> ... char buf[20]; size_t nbytes; ssize_t bytes_read; int fd; ... nbytes = sizeof(buf); bytes_read = read(fd, buf, nbytes); ...
Awarding USAGE
None.
RATIONALE
This volume of POSIX.1-2017 does non specify the value of the file offset after an error is returned; there are as well many cases. For programming errors, such every bit [EBADF], the concept is meaningless since no file is involved. For errors that are detected immediately, such as [EAGAIN], clearly the offset should not change. After an interrupt or hardware fault, all the same, an updated value would be very useful and is the beliefs of many implementations.
Annotation that a read() of zero bytes does not alter the last information access timestamp. A read() that requests more than zero bytes, just returns zero, is required to modify the last data access timestamp.
Implementations are allowed, but not required, to perform fault checking for read() requests of nil bytes.
Input and Output
The utilise of I/O with large byte counts has always presented issues. Ideas such as lread() and lwrite() (using and returning longs) were considered at i time. The current solution is to use abstract types on the ISO C standard function to read() and write(). The abstract types tin exist declared then that existing functions work, just can also be declared and then that larger types tin be represented in future implementations. It is presumed that any constraints limit the maximum range of size_t as well limit portable I/O requests to the same range. This book of POSIX.1-2017 also limits the range farther by requiring that the byte count exist limited and then that a signed return value remains meaningful. Since the return type is as well a (signed) abstract type, the byte count tin exist defined by the implementation to exist larger than an int can hold.
The standard developers considered calculation atomicity requirements to a piping or FIFO, only 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 be an assistance to applications portability.
This volume of POSIX.one-2017 requires that no action be taken for read() or write() when nbyte is zero. 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 POSIX.one-2017, but the phrasing here could be misread to require detection of the zero case before whatsoever other errors. A value of zero is to be 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 unmarried operation that started out together end up together, without interleaving from other I/O operations. Information technology is a known aspect of terminals that this is not honored, and terminals are explicitly (and implicitly permanently) excepted, making the beliefs unspecified. The behavior for other device types is besides left unspecified, just the diction is intended to imply that future standards might choose to specify atomicity (or not).
There were recommendations to add format parameters to read() and write() in order to handle networked transfers amidst heterogeneous file organization and base of operations hardware types. Such a facility may be required for support by the OSI presentation of layer services. However, it was determined that this should correspond with similar C-language facilities, and that is beyond the scope of this volume of POSIX.ane-2017. The concept was suggested to the developers of the ISO C standard for their consideration as a possible area for futurity work.
In four.3 BSD, a read() or write() that is interrupted by a betoken earlier transferring whatever data does not by default return an [EINTR] error, but is restarted. In 4.2 BSD, 4.iii BSD, and the Eighth Edition, there is an additional function, select(), whose purpose is to pause until specified activity (data to read, space to write, and then 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 equally keyboard input) where interruption of I/O due to a point is desired.
The effect of which files or file types are interruptible is considered an implementation design issue. This is ofttimes affected primarily by hardware and reliability problems.
In that location are no references to actions taken following an "unrecoverable error". It is considered beyond the scope of this volume of POSIX.ane-2017 to depict what happens in the case of hardware errors.
Earlier versions of this standard allowed two very unlike behaviors with regard to the handling of interrupts. In order to minimize the resulting confusion, information technology was decided that POSIX.1-2017 should support only one of these behaviors. Historical practice on AT&T-derived systems was to have read() and write() return -1 and set errno to [EINTR] when interrupted subsequently some, but not all, of the information requested had been transferred. Withal, the U.s.a. Department of Commerce FIPS 151-i and FIPS 151-2 require the historical BSD behavior, in which read() and write() return the number of bytes actually transferred earlier the interrupt. If -one is returned when whatever data is transferred, it is difficult to recover from the error on a seekable device and impossible on a non-seekable device. Almost new implementations support this beliefs. The beliefs required by POSIX.1-2017 is to return the number of bytes transferred.
POSIX.1-2017 does non specify when an implementation that buffers read()s actually moves the data into the user-supplied buffer, so an implementation may choose to exercise this at the latest possible moment. Therefore, an interrupt arriving before may not cause read() to render a partial byte count, only rather to return -1 and gear up errno to [EINTR].
Consideration was besides given to combining the ii previous options, and setting errno to [EINTR] while returning a short count. However, not simply is there no existing practice that implements this, it is besides contradictory to the idea that when errno is set, the function responsible shall return -one.
This volume of POSIX.1-2017 intentionally does not specify whatsoever pread() errors related to pipes, FIFOs, and sockets other than [ESPIPE].
FUTURE DIRECTIONS
None.
SEE ALSO
fcntl, ioctl, lseek, open, pipe, readv
XBD General Last Interface, <stropts.h>, <sys/uio.h>, <unistd.h>
CHANGE HISTORY
Outset released in Issue 1. 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.
Large File Superlative extensions are added.
The pread() function is added.
Issue six
The Clarification and ERRORS sections are updated and so that references to STREAMS are marked as office of the XSI STREAMS Option 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 past a signal subsequently information technology has successfully read some data, information technology returns the number of bytes read. In Outcome 3, 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 Description, text is added to indicate that for regular files, no data transfer occurs past the showtime maximum established in the open file description associated with fildes. This change is to back up big files.
The [EOVERFLOW] mandatory error condition is added.
The [ENXIO] optional error condition is added.
Text referring to sockets is added to the Description.
The following changes were made to align with the IEEE P1003.1a draft standard:
The effect of reading zero bytes is clarified.
The DESCRIPTION is updated for alignment with IEEE Std 1003.1j-2000 by specifying that read() results are unspecified for typed retention objects.
New RATIONALE is added to explain the atomicity requirements for input and output operations.
The following error conditions are added for operations on sockets: [EAGAIN], [ECONNRESET], [ENOTCONN], and [ETIMEDOUT].
The [EIO] error is made optional.
The following error weather are added for operations on sockets: [ENOBUFS] and [ENOMEM].
The readv() part is split out into a divide reference page.
IEEE Std 1003.i-2001/Cor two-2004, item XSH/TC2/D6/108 is applied, updating the [EAGAIN] mistake in the ERRORS section from "the procedure would be delayed" to "the thread would exist delayed".
IEEE Std 1003.1-2001/Cor 2-2004, item XSH/TC2/D6/109 is applied, making an editorial correction in the RATIONALE section.
Issue vii
The pread() function is moved from the XSI option to the Base of operations.
Functionality relating to the XSI STREAMS option is marked obsolescent.
Changes are made related to support for finegrained timestamps.
POSIX.1-2008, Technical Blunder i, XSH/TC1-2008/0480 [218], XSH/TC1-2008/0481 [79], XSH/TC1-2008/0482 [218], XSH/TC1-2008/0483 [218], XSH/TC1-2008/0484 [218], and XSH/TC1-2008/0485 [218,428] are applied.
POSIX.one-2008, Technical Corrigendum 2, XSH/TC2-2008/0302 [710] and XSH/TC2-2008/0303 [676,710] are practical.
Stop of informative text.
return to height of folio
POSIX ™ is a Trademark of The IEEE.
Copyright © 2001-2018 IEEE and The Open up Group, All Rights Reserved
[ Main Index | XBD | XSH | XCU | XRAT ]
rappaportjoher1941.blogspot.com
Source: https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html
0 Response to "How to Read Between Two Bytes Offset File C++"
Post a Comment