Function Definition
Before defining the read function in your code, you have to include some required packages.
Here is how you define the POSIX read function:
>> ssize_t read(int fd, void *buf, size_t nbytes);
Three parameter arguments can be taken from the read method call:
int fd: The file descriptor of the file from where the information is to be read. We could either be using a file descriptor acquired via an open system call, or we could just use 0, 1, or 2 referring to typical input, regular output, or regular error, respectively.
Void *buf: The buffer or character array in which the read data should be saved and kept.
Size_t nbyte: The number of bytes that needed to be read from the document before truncating. All information can be stored in the buffer if the information to be read is shorter than nbytes.
Description
The read() method tries to read ‘nbyte’ bytes into the buffer cache referred to by ‘buf’ from either the file connected with the open document descriptor ‘Fildes’ or ‘fd’. It does not define the nature of several simultaneous reads on the same stream, FIFO, or terminal unit.
On documents that enable the reading, the reading process begins at the offset of the document, and the offset is increased by the number of bytes read. If the document offset is at or beyond the file’s edge, there are no bytes read, and read() yields none.
When the count is 0, read() will recognize the errors mentioned below. If there are no mistakes, or if read() is not accounted for with Errors, a read() yields zero with a count of 0 and therefore has no other repercussions.
If the count is higher than SSIZE_MAX, as per POSIX.1, then the outcome is determined by the implementation.
Return Value
The numeral of bytes ‘read’ and ‘pread’ reverted upon achievement must be a non-negative integer while zero points to the end of the file. The document position is progressed by this number, or else, to signify an error, the methods return -1 and assign ‘errno’. When this figure is less than the number of bytes requested, it is not a mistake byte. It could be possible that fewer bytes are available for now.
Errors
The pread and read function will be unsuccessful if these errors occur:
EAGAIN:
The document or file descriptor ‘fd’ belongs to a non-socket file that has been labeled as non-blocking (O NONBLOCK) and will block the reading.
EWOULDBLOCK:
The descriptor ‘fd’ belongs to a socket that has been labeled as non-blocking (O_NONBLOCK) and will block the reading.
EBADF:
The ‘fd’ may not be a usable descriptor, or it may not be open for reading.
EFAULT:
This happens when your ‘buf’ is outside your reachable address space.
EINTR:
Before the reading of information data, the call may have broken up by a signal.
EINVAL:
This error occurs when your ‘fd’ descriptor is involved in an object, which is not appropriate for reading, or the document was untied with the O_DIRECT flag, and one or the other address stated in ‘buf’, the value indicated in ‘count’, or the document offset is not appropriately associated.
EINVAL:
The descriptor ‘fd’ may have been formed using a call to timerfd_create(2), and the incorrect size buffer has been given to read.
EIO:
It is an input/output error. It occurs when the background process group attempts to read from its regulatory terminal, and one or the other is overlooking or blocking SIGTTIN, or its process group is bereaved. Another reason for this error could be low-level input/output error meanwhile reading from a hard disk or tape. Another potential cause of EIO on networked data files is the removal of advisory locking on the file descriptor and the failure of that lock.
EISDIR:
The file descriptor ‘fd’ belongs to a directory.
Notes:
Many other errors may also occur, contingent on the object linked to descriptor ‘fd’. Both size_t and ssize_t forms are unmarked and marked numerical data types defined by POSIX.1. On Linux, at most 0x7ffff000 (2,147,479,552) bytes can be transmitted by reading function (and equivalent system calls), returning the number of bytes originally transmitted (on both 32-bit and 64-bit platforms). With NFS filesystems, just the first moment the timestamp is changed by reading tiny streams of information, subsequent calls wouldn’t do so. It is triggered by caching of client-side attributes since, although not all, NFS clients quit updating to the server via st_atime (last file access time) and client-side reads fulfilled from the buffer of the client would not trigger changes to st-atime on the server as no server-side readings are available. By removing client-side attribute caching, UNIX metadata may be accessed, but this would significantly increase the load on the server and affect productivity in most cases.
Example 01:
Here is a C program to demonstrate the read function call on the Linux System. Write the below-command as it is in a new file. Add libraries, and in the main function, initialize a descriptor and size. The descriptor is opening the file, and size is used to read file data.
The output for the above-code would be as shown in the below image.
Example 02:
Another example to illustrate the working of the read function is given below.
Create another file and write down the code below as it is in it. Here are two descriptors, fd1 & fd2, that both have their own open table file access. So for foobar.txt, every descriptor does have its file location. The very first byte of foobar.txt is translated from fd2, and the result is c = f, not c = o.
Conclusion
We have read the POSIX read function in C programming efficiently. Hopefully, there are no doubts left.