pwrite64()--Write to Descriptor with Offset (large file enabled)


  Syntax
 #include <unistd.h>

 ssize_t pwrite64
   (int file_descriptor, const void *buf,
    size_t nbyte, off64_t offset);    
  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes.

The pwrite64() 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.

The offset will also be ignored if file_descriptor refers to a descriptor obtained using the open() function with O_APPEND specified.

pwrite64() is enabled for large files. It is capable of operating on files larger than 2GB minus 1 byte as long as the file has been opened by either of the following:

For additional information about parameters, authorities, and error conditions, see pwrite()--Write to Descriptor with Offset.


Usage Notes

  1. When you develop in C-based languages, the prototypes for the 64-bit APIs are normally hidden. To use the pwrite64 API, you must compile the source with the _LARGE_FILE_API macro defined.
  2. All of the usage notes for pwrite() apply to pwrite64(). See Usage Notes in the pwrite API.

Example

See Code disclaimer information for information pertaining to code examples.

The following example writes a specific number of bytes to a file:

#define _LARGE_FILE_API
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

#define mega_string_len 1000000

main() {
  char *mega_string;
  int  file_descriptor;
  int  ret;
  off64_t  off=5;
  char fn[]="write.file";

  if ((mega_string = (char*) malloc(mega_string_len+off)) == NULL)
    perror("malloc() error");
  else if ((file_descriptor = creat64(fn, S_IWUSR)) < 0)
    perror("creat64() error");
  else {
    memset(mega_string, '0', mega_string_len);
    if ((ret = pwrite64(file_descriptor, mega_string, mega_string_len, off)) == -1)
      perror("pwrite64() error");
    else printf("pwrite64() wrote %d bytes at offset %d\n", ret, off);
    if (close(file_descriptor)!= 0)
       perror("close() error");
    if (unlink(fn)!= 0)
       perror("unlink() error");
  }
  free(mega_string);
}

Output:

pwrite64() wrote 1000000 bytes at offset 5

API introduced: V5R2
Top | UNIX-Type APIs | APIs by category