pwrite(), pwrite64(), write()

write to file 

Function


SYNOPSIS

#include <unistd.h>

ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset);

ssize_t pwrite64(int fildes, const void *buf, size_t nbyte, off64_t offset);

ssize_t write(int fildes, const void *buf, size_t nbyte);


DESCRIPTION

The write() function attempts to write the specified number of bytes from the specified buffer to the specified file descriptor.

If the number of bytes is 0, write() returns 0 and have no other results if the file is a regular file; otherwise the results are unspecified.

On a regular file or other file capable of seeking, the actual writing of data proceeds from the position in the file indicated by the file offset associated with the specified file descriptor. Before successful return from write(), the file offset is incremented by the number of bytes actually written. On a regular file, if this incremented file offset is greater than the length of the file, the length of the file is set to this file offset.

On a file not capable of seeking, writing always takes place starting at the current position. The value of a file offset associated with such a device is undefined.

If the O_APPEND flag is set for the file descriptor, the file offset is set to the end of the file prior to each write and no intervening file modification operation occurs between changing the file offset and the write operation.

If a write() requests more bytes be written than there is room for (for example, the physical end of the medium), only as many bytes as there is room for are written.

If write() is interrupted by a signal before it writes any data, it returns -1 with errno set to EINTR. If write() is interrupted by a signal after it successfully writes some data, it returns the number of bytes written.

Writes larger than SSIZE_MAX are unsupported.

After a write() to a regular file has successfully returned:

Write requests to a pipe or FIFO are handled the same as a regular file with the following exceptions:

When attempting to write to a file descriptor (other than a pipe or FIFO) that supports non-blocking writes and cannot accept data immediately:

If the O_SYNC bit has been set, write I/O operations on the file descriptor complete only after the data has been flushed to the mass storage device.

Upon successful completion, when the number of requested bytes is greater than 0, write() marks for update the st_ctime and st_mtime fields of the file.

The pwrite() function is identical to the write() function except that it starts writing at a point in the file indicated by the offset parameter.

The pwrite64() function is identical to the pwrite() function except that it uses the larger off64_t in order to manipulate the file position of files that are larger than 2 gigabytes.


PARAMETERS

fildes 

Is the file descriptor of an open file to write to.

buf 

Is an array of data to write to the open file.

nbyte 

Is the number of bytes to write to the file.

offset 

Specifies the point in the file where pwrite() or pwrite64() begins writing.


RETURN VALUES

If successful, these functions return the number of bytes written to the open file descriptor fildes. The number of bytes written never exceeds nbyte. Otherwise, these functions return -1 and set errno to one of the following values:

EAGAIN 

The O_NONBLOCK flag is set for the file descriptor and the thread would be delayed in the write() operation.

EBADF 

The fildes parameter is not a valid file descriptor open for writing.

EFAULT 

The buf parameter is not a valid pointer.

EFAULT 

buf points outside the allocated address space of the process.

EFBIG 

An attempt is made to write a file that exceeds the file size limit of the process.

EFBIG 

The file is a regular file, nbyte 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 fildes.

EINTR 

A signal interrupted the call to write(), pwrite(), or pwrite64().

EIO 

A physical I/O error has occurred.

ENOMEM 

Insufficient memory exists to complete the request.

ENOSPC 

There is no more free space on the device that fildes refers to.

ENXIO 

A request was made of a non-existent device, or the request was outside the capabilities of the device.

EOVERFLOW 

For pwrite() and pwrite64(), the specified offset would cause a write beyond the 2 GB boundary.

EPIPE 

An attempt is made to write to a pipe or FIFO that is not open for reading by any process, or that has only one end open. A SIGPIPE is also sent to the thread.


CONFORMANCE

POSIX.1 (1996), with exceptions. UNIX 03, with exceptions.


MULTITHREAD SAFETY LEVEL

Async-signal-safe.


PORTING ISSUES

Refer to File Management in the Windows Concepts chapter of the PTC MKS Toolkit UNIX to Windows Porting Guide for a detailed discussion of file handling, including a discussion of text mode and binary mode for files.

The test which causes the EFBIG error condition to be returned can be disabled by using the _NutConf() function to turn off the _NC_SET_FULL_LFS_SEMANTICS flag.

While the UNIX 03 specification states that pwrite() does not change the file pointer, the underlying Win32 APIs used to implement the function on the NuTCRACKER Platform do adjust the file pointer. As a result it is recommended that you not mix pwrite() (or pwrite64()) and write() in the same application.


AVAILABILITY

PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
creat(), dup(), dup2(), fcntl(), ioctl(), open(), pipe(), read(), readv(), socket(), writev()

Miscellaneous:
lf64


PTC MKS Toolkit 10.5 Documentation Build 40.