pwrite(), pwrite64()

Updated: April 19, 2023

Write into a file without changing the file pointer

Synopsis:

#include <unistd.h>

ssize_t pwrite( int filedes,
                const void* buff,
                size_t nbytes,
                off_t offset );

ssize_t pwrite64( int filedes,
                  const void* buff,
                  size_t nbytes,
                  off64_t offset );

Arguments:

filedes
The file descriptor for the file you want to write in.
buff
A pointer to a buffer that contains the data you want to write.
nbytes
The number of bytes that you want to write. This amount must not exceed SSIZE_MAX - sizeof(io_write_t) - sizeof(struct _xtype_offset) (see <limits.h>), or the function fails and sets errno to EOVERFLOW.
offset
The desired position inside the file.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The pwrite() and pwrite64() functions perform the same action as write(), except that they write into a given position without changing the file pointer. The pwrite64() function is a large-file support version of pwrite().

Note: In QNX Neutrino 6.6 or later, the large-file support functions and data types appear in the name space only if you define _LARGEFILE64_SOURCE when you compile your code. For more information, see Classification in What's in a Function Description?

Returns:

The number of bytes actually written, or -1 if an error occurred (errno is set).

Errors:

EAGAIN
The O_NONBLOCK flag is set for the file descriptor, and the process would be delayed in the write operation.
EBADF
The file descriptor, filedes, isn't a valid file descriptor open for writing.
EFBIG
One of the following occurred:
  • An attempt was made to write a file that exceeds the maximum file size and there was no room for any bytes to be written.
  • The file is a regular file, nbytes is greater than 0, and the starting position is greater than or equal to the offset maximum established in the open file description associated with filedes.
EINTR
The write operation was interrupted by a signal, and either no data was transferred, or the resource manager responsible for that file doesn't report partial transfers.
EINVAL
The offset argument is invalid; the value is negative.
EIO
One of the following occurred:
  • The process is a member of a background process group attempting to write to its controlling terminal, TOSTOP is set, the process is neither ignoring nor blocking SIGTTOU, and the process group of the process is orphaned.
  • A physical I/O error occurred (for example, a bad block on a disk). The precise meaning is device-dependent.
ENOBUFS
Insufficient resources were available in the system to perform the operation.
ENOSPC
There's no free space remaining on the device containing the file.
ENOSYS
The pwrite() function isn't implemented for the device specified by filedes.
ENXIO
One of the following occurred:
  • A request was made of a nonexistent device, or the request was outside the capabilities of the device.
  • A hangup occurred on the STREAM being written to.
EOVERFLOW
An attempt was made to write an amount of data that when added to the sizes of the write message and extra type offset structures exceeds the allowable limit.
EPIPE
An attempt was made to write to a pipe (or FIFO) that isn't open for reading by any process. A SIGPIPE signal is also sent to the process.
ERANGE
The transfer request size was outside the range supported by the STREAMS file associated with filedes.
ESPIPE
The filedes argument is associated with a pipe, FIFO, or socket. The file pointer remains unchanged.

Classification:

pwrite() is POSIX 1003.1; pwrite64() is Large-file support

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes