Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of pwrite and pread over fwrite and fread?

Tags:

c++

posix

Hey please don't mind if I am asking trivial question, but, please can somebody help me with this..

like image 311
Invictus Avatar asked Sep 29 '11 05:09

Invictus


People also ask

What is Pread and Pwrite?

DESCRIPTION. pread() reads up to count bytes from file descriptor fd at offset offset (from the start of the file) into the buffer starting at buf. The file offset is not changed. pwrite() writes up to count bytes from the buffer starting at buf to the file descriptor fd at offset offset. The file offset is not changed ...

What is the difference between read and Pread?

The pread() function performs the same action as read(), except that it reads from a given position in the file without changing the file pointer. The first three arguments to pread() are the same as read(), with the addition of a fourth argument offset for the desired position inside the file.

What is Pwrite?

Pwrite is a term used to describe the highest power setting for the laser writing to a CD-RW disc.

What is Pwrite in Linux?

The pwrite() function writes nbyte bytes from buf to the file associated with file_descriptor. The offset value defines the starting position in the file and the file pointer position is not changed.


1 Answers

There are two parts:

  1. Difference between pread/pwrite and read/write:

    They are both at the same level, namely system calls. There are two differences:

    1. The "p" variants take offset to read from, so they are independent of the current file pointer. That makes it easier to read/write from multiple threads concurrently.
    2. The "p" variants only work on seekable files (i.e. real files, not pipes, sockets or devices).
  2. Difference between read/pread/write/pwrite and fread/fwrite:

    The "f" variants are standard runtime wrappers of the former (using the basic variants). They support in-process buffering. That can significantly improve performance for simple code, but it makes use of other features of the system-call level impractical.

Only use the "p" variants if you have good use for reading at random offsets (avoiding seeks and allowing concurrent access via one file handle), which often the case with some kind of database files (record-oriented with records at known offsets) and rarely in other applications.

like image 161
Jan Hudec Avatar answered Oct 08 '22 22:10

Jan Hudec