Posix Call to Read Data From a File

In traditional POSIX compatible operating systems, to get data from a document independent in a file system, a program used the read system phone call. A document descriptor that is usually accessed from a prior phone call to open up is defined by the file. This read system telephone call reads out the information in bytes and the integer of which the caller specifies from the document, and then saves it in a buffer provided by the calling mechanism.

Role Definition

Before defining the read part in your code, you take to include some required packages.

Here is how you define the POSIX read role:

>> ssize_t pread( int fildes, void *buf, size_t nbyte, off_t beginning) ;
>> 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 caused via an open arrangement telephone 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 certificate 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 continued with the open up certificate descriptor 'Fildes' or 'fd'. Information technology does not define the nature of several simultaneous reads on the same stream, FIFO, or concluding unit.

On documents that enable the reading, the reading process begins at the offset of the document, and the offset is increased past the number of bytes read. If the certificate offset is at or beyond the file's border, at that place 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 non 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 adamant by the implementation.

Render Value

The numeral of bytes 'read' and 'pread' reverted upon achievement must be a non-negative integer while zero points to the cease of the file. The document position is progressed by this number, or else, to signify an fault, the methods return -one and assign 'errno'. When this effigy is less than the number of bytes requested, information technology is not a mistake byte. It could be possible that fewer bytes are bachelor for at present.

Errors

The pread and read function will exist unsuccessful if these errors occur:

EAGAIN:

The document or file descriptor 'fd' belongs to a non-socket file that has been labeled every bit non-blocking (O NONBLOCK) and volition block the reading.

EWOULDBLOCK:

The descriptor 'fd' belongs to a socket that has been labeled as not-blocking (O_NONBLOCK) and will block the reading.

EBADF:

The 'fd' may not exist 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 past a signal.

EINVAL:

This mistake 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 certificate showtime is non appropriately associated.

EINVAL:

The descriptor 'fd' may have been formed using a phone call to timerfd_create(ii), 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 mistake could be depression-level input/output error meanwhile reading from a hd 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 past POSIX.1. On Linux, at most 0x7ffff000 (ii,147,479,552) bytes can exist transmitted by reading part (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 inverse by reading tiny streams of information, subsequent calls wouldn't do so. It is triggered past caching of customer-side attributes since, although not all, NFS clients quit updating to the server via st_atime (last file admission fourth dimension) and client-side reads fulfilled from the buffer of the customer would not trigger changes to st-atime on the server as no server-side readings are bachelor. Past removing client-side attribute caching, UNIX metadata may be accessed, merely this would significantly increment the load on the server and affect productivity in near cases.

Example 01:

Here is a C programme to demonstrate the read function call on the Linux System. Write the beneath-control 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 higher up-code would exist as shown in the below prototype.

Example 02:

Another case to illustrate the working of the read function is given below.

Create another file and write down the code beneath as it is in it. Hither are two descriptors, fd1 & fd2, that both take their own open tabular array file access. Then 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 take read the POSIX read role in C programming efficiently. Hopefully, there are no doubts left.

About the author

I am a self-motivated it professional person with a passion for writing. I am a technical writer and dearest to write for all Linux flavors and Windows.

morrislounctirough.blogspot.com

Source: https://linuxhint.com/posix-read-functioning-in-c-programming/

0 Response to "Posix Call to Read Data From a File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel