#include <unistd.h> ssize_t pwrite (int file_descriptor, const void *buf, size_t nbyte, off_t offset);Service Program Name: QP0LLIB1
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.
See write()--Write to Descriptor for more information relating to writing to a descriptor.
The offset will also be ignored if file_descriptor refers to a descriptor obtained using the open() function with O_APPEND specified.
No authorization is required.
value | pwrite() was successful. The value returned is the number of bytes actually written. This number is less than or equal to nbyte. |
-1 | pwrite() was not successful. The errno global variable is set to indicate the error. |
If pwrite() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.
Error condition | Additional information |
---|---|
[EACCES] |
If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems. |
[EAGAIN] | |
[EBADF] | |
[EBADFID] | |
[EBUSY] | |
[EDAMAGE] | |
[EFAULT] | |
[EFBIG] |
The size of the object would exceed the system allowed maximum size or the process soft file size limit. The file is a regular file, nbyte is greater than 0, and the starting offset is greater than or equal to 2 GB minus 2 bytes. |
[EINTR] | |
[EINVAL] |
The file system that the file resides in does not support large files, and the starting offset exceeds 2GB minus 2 bytes. This will also occur if the offset value is less than 0. |
[EIO] | |
[EJRNDAMAGE] | |
[EJRNENTTOOLONG] | |
[EJRNINACTIVE] | |
[EJRNRCVSPC] | |
[ENEWJRN] | |
[ENEWJRNRCV] | |
[ENOMEM] | |
[ENOSPC] | |
[ENOTAVAIL] | |
[ENOTSAFE] | |
[ENXIO] | |
[ERESTART] | |
[ETRUNC] | |
[ESPIPE] | |
[ESTALE] |
If you are accessing a remote file through the Network File System, the file may have been deleted at the server. |
[EUNKNOWN] |
If interaction with a file server is required to access the object, errno could also indicate one of the following errors:
Error condition | Additional information |
---|---|
[EADDRNOTAVAIL] | |
[ECONNABORTED] | |
[ECONNREFUSED] | |
[ECONNRESET] | |
[EHOSTDOWN] | |
[EHOSTUNREACH] | |
[ENETDOWN] | |
[ENETRESET] | |
[ENETUNREACH] | |
[ESTALE] |
If you are accessing a remote file through the Network File System, the file may have been deleted at the server. |
[ETIMEDOUT] | |
[EUNATCH] |
The following messages may be sent from this function:
Message ID | Error Message Text |
---|---|
CPE3418 E | Possible APAR condition or hardware failure. |
CPF3CF2 E | Error(s) occurred during running of &1 API. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
CPFA081 E | Unable to set return value or error code. |
CPFA0D4 E | File system error occurred. Error number &1. |
For a physical file member opened in text mode, the offset must be equal to the current file position. Otherwise, this operation will fail with error code [EIO].
This function will fail with error code [ENOTSAFE] if the object on which this function is operating is a save file and multiple threads exist in the job.
If the file specified is a save file, only complete records will be written into the save file. A pwrite() request that does not provide enough data to completely fill a save file record will cause the partial record's data to be saved by the file system. The saved partial record will then be combined with additional data on subsequent pwrite()'s until a complete record may be written into the save file. If the save file is closed prior to a saved partial record being written into the save file, then the saved partial record is discarded, and the data in that partial record will need to be written again by the application.
A successful pwrite() updates the change, modification, and access times for a database member using the normal rules that apply to database files. At most, the access time is updated once per day.
You should be careful when writing end-of-file characters in the QSYS.LIB
and independent ASP QSYS.LIB file systems. For these file systems, end-of-file
characters are symbolic; that is, they are stored outside the file member.
However, some situations can result in actual, nonsymbolic end-of-file
characters being written to a member. These nonsymbolic end-of-file characters
could cause some tools or utilities to fail. For example:
The change and modification times of the file are updated when the file is closed.
When writing to files on volumes formatted in Universal Disk Format (UDF), byte locks on the range being written are ignored.
Local access to remote files through the Network File System may produce unexpected results due to conditions at the server. Once a file is open, subsequent requests to perform operations on the file can fail because file attributes are checked at the server on each request. If permissions on the file are made more restrictive at the server or the file is unlinked or made unavailable by the server for another client, your operation on an open file descriptor will fail when the local Network File System receives these updates. The local Network File System also impacts operations that retrieve file attributes. Recent changes at the server may not be available at your client yet, and old values may be returned from operations (several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data).
Reading and writing to files with the Network File System relies on byte-range locking to guarantee data integrity. To prevent data inconsistency, use the fcntl() API to get and release these locks.
The largest buffer size allowed is 16 megabytes. If a larger buffer is passed, the error EINVAL will be received.
See Code disclaimer information for information pertaining to code examples.
The following example writes a specific number of bytes to a file:
#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; off_t off=5; char fn[]="write.file"; if ((mega_string = (char*) malloc(mega_string_len+off)) == NULL) perror("malloc() error"); else if ((file_descriptor = creat(fn, S_IWUSR)) < 0) perror("creat() error"); else { memset(mega_string, '0', mega_string_len); if ((ret = pwrite(file_descriptor, mega_string, mega_string_len, off)) == -1) perror("pwrite() error"); else printf("pwrite() 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:
pwrite() wrote 1000000 bytes at offset 5
Top | UNIX-Type APIs | APIs by category |