ibm-information-center/dist/eclipse/plugins/i5OS.ic.apis_5.4.0.1/write.htm

1176 lines
31 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
<title>write()--Write to Descriptor</title>
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<!-- Begin Header Records --><!-- file cleaned -->
<!-- Unix2 SCRIPT J converted by B2H R4.1 (346) (CMS) by V2KEA304 -->
<!-- at RCHVMW2 on 17 Feb 1999 at 11:05:09 -->
<!-- Change History: -->
<!-- 010307 JTROUS Change to xref pread, pread64, pwrite, -->
<!-- pwrite64 for V5R2, DCR 98686, also include -->
<!-- changes from 98391, first PASE I0 for V5R2 -->
<!-- 010320 JTROUS Include Journal ERRNOs, should have in V5R1 -->
<!-- 010703 JTROUS Add ERESTART, EINTR, V5R2, DCR 98686, add dev/zero-->
<!-- 011022 JTROUS Changes from API Review 1, V5R2 -->
<!-- 020510 RTHEIS Add fclear() & fclear64() APIs, V5R3 -->
<!-- 020618 EMIG: updated for NFS threadsafety, V5R3 -->
<!-- 0206?? JET This file has undergone html cleanup -->
<!-- 020718 MFENLON: updated for QFileSvr.400 threadsafety, V5R3 -->
<!-- 050325 JTROUS: fix enums, no change flag, V5R4 -->
<!-- End Header Records -->
<!-- Edited by Kersten Feb 02 -->
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body>
<!-- Java sync-link -->
<script type="text/javascript" language="Javascript" src="../rzahg/synch.js">
</script>
<a name="Top_Of_Page"></a>
<h2>write()--Write to Descriptor</h2>
<div class="box" style="width: 70%;">
<br>
&nbsp;&nbsp;Syntax<br>
<pre>
#include &lt;unistd.h&gt;
ssize_t write
(int <em>file_descriptor</em>, const void <em>*buf</em>, size_t <em>nbyte</em>);
</pre>
<br>
&nbsp;&nbsp;Service Program Name: QP0LLIB1<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Default Public Authority: *USE<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Threadsafe: Conditional; see <a href="#USAGE_NOTES">Usage Notes</a>.<br>
<!-- iddvc RMBR -->
<br>
</div>
<p>The <strong>write()</strong> function writes <em>nbyte</em> bytes from <em>
buf</em> to the file or socket associated with <em>file_descriptor</em>. <em>
nbyte</em> should not be greater than INT_MAX (defined in the
&lt;<strong>limits.h</strong>&gt; header file). If <em>nbyte</em> is zero,
<strong>write()</strong> simply returns a value of zero without attempting any
other action.</p>
<p>If <em>file_descriptor</em> refers to a "regular file" (a stream file that
can support positioning the file offset) or any other type of file on which the
job can do an <strong>lseek()</strong> operation, <strong>write()</strong>
begins writing at the file offset associated with <em>file_descriptor</em>,
unless O_APPEND is set for the file (see below). A successful <strong>
write()</strong> increments the file offset by the number of bytes written. If
the incremented file offset is greater than the previous length of the file,
the length of the file is set to the new file offset.</p>
<p>If O_APPEND (defined in the &lt;<strong>fcntl.h</strong>&gt; header file) is
set for the file, <strong>write()</strong> sets the file offset to the end of
the file before writing the output.</p>
<p>If there is not enough room to write the requested number of bytes (for
example, because there is not enough room on the disk), the <strong>
write()</strong> function writes as many bytes as the remaining space can
hold.</p>
<p>If <strong>write()</strong> is successful and <em>nbyte</em> is greater than
zero, the change and modification times for the file are updated.</p>
<p>If <em>file_descriptor</em> refers to a descriptor obtained using the
<strong>open()</strong> function with O_TEXTDATA specified, the data is written
to the file assuming it is in textual form. The maximum number of bytes on a
single write that can be supported for text data is 2,147,483,408 (2GB - 240)
bytes. The data is converted from the code page of the application, job, or
system to the code page of the file as follows:</p>
<ul>
<li>When writing to a true stream file, any line-formatting characters (such as
carriage return, tab, and end-of-file) are just converted from one code page to
another.<br>
<br>
</li>
<li>When writing to a record file that is being used as a stream file:<br>
<br>
<ul>
<li>End-of-line characters are removed.</li>
<li>Records are padded with blanks (for a source physical file member) or nulls
(for a data physical file member).</li>
<li>Tab characters are replaced by the appropriate number of blanks to the next
tab position.</li>
</ul>
</li>
</ul>
<p>There are some important considerations if O_CCSID was specified on the
<strong>open()</strong>.</p>
<ul>
<li>The <strong>write()</strong> will attempt to convert all of the data in the
user's buffer. Successfully converted data will be written. Unconverted data is
usually assumed to be a partial character. Partial characters will be buffered
internally and data from the next consecutive write will be appended to the
buffered data. If incorrect data is provided on a consecutive write, the write
may fail with the [ECONVERT] error.
<p>If an <strong>lseek()</strong> is performed, the file is closed, or the
current job is ended, the buffered data will be discarded. Discarded data will
not be written to the file. See <a href="lseek.htm">lseek()</a>--Set File
Read/Write Offset for more information.</p>
</li>
<li>Because of the above consideration and because of the possible expansion or
contraction of converted data, applications using the O_CCSID flag should avoid
assumptions about data size and the current file offset. For example, the user
may supply a buffer to 100 bytes, but after an application has written the
buffer to a new file, the file size may be 50, 200, or something else,
depending on the CCSIDs involved.</li>
</ul>
<p>If O_TEXTDATA was not specified on the <strong>open()</strong>, the data is
written to the file without conversion. The application is responsible for
handling the data.</p>
<p>When <em>file_descriptor</em> refers to a socket, the <strong>
write()</strong> function writes to the socket identified by the socket
descriptor.</p>
<p><strong>Note:</strong> When the write completes successfully, the S_ISUID
(set-user-ID) and S_ISGID (set-group-ID) bits of the file mode will be cleared.
If the write is unsuccessful, the bits are undefined.</p>
<p>Write requests to a pipe or FIFO are handled the same as a regular file,
with the following exceptions:</p>
<ul>
<li>The S_ISUID and S_ISGID file mode bits will not be cleared.<br>
<br>
</li>
<li>There is no file offset associated with a pipe or FIFO. Each write request
will append to the end of the pipe or FIFO.<br>
<br>
</li>
<li>Write requests of [PIPE_BUF] bytes or less will not be interleaved with
data from other threads performing writes on the same pipe or FIFO. Writes of
greater than [PIPE_BUF] bytes may have data interleaved on arbitrary boundaries
with writes by other threads, whether or not the O_NONBLOCK flag of the file
status flags is set.<br>
<br>
</li>
<li>If the O_NONBLOCK flag was not specified and the pipe or FIFO is full, the
write request will block the calling thread until the requested amount of data
in <em>nbyte</em> is written.<br>
<br>
</li>
<li>If the O_NONBLOCK flag was specified, then the following pertain to various
write requests:<br>
<br>
<ul>
<li>The <strong>write()</strong> function will not block the calling
thread.<br>
<br>
</li>
<li>A write request for [PIPE_BUF] or fewer bytes will have the following
effect:
<p>If there is sufficient space available in the pipe or FIFO, <strong>
write()</strong> will transfer all the data and return the number of bytes
requested. If there is not sufficient space in the pipe or FIFO, <strong>
write()</strong> will transfer no data, return -1, and set <em>errno</em> to
[EAGAIN].</p>
</li>
<li>A write request for more than [PIPE_BUF] bytes will cause one of the
following:
<ul>
<li>When at least one byte can be written, <strong>write()</strong> will
transfer what it can and return the number of bytes written.<br>
<br>
</li>
<li>When no data can be written, <strong>write()</strong> will transfer no
data, return -1, and set <em>errno</em> to [EAGAIN].</li>
</ul>
</li>
</ul>
</li>
</ul>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong>file_descriptor</strong></dt>
<dd>(Input) The descriptor of the file to which the data is to be written.<br>
<br>
</dd>
<dt><strong>buf</strong></dt>
<dd>(Input) A pointer to a buffer containing the data to be written.<br>
<br>
</dd>
<dt><strong>nbyte</strong></dt>
<dd>(Input) The size in bytes of the data to be written.</dd>
</dl>
<br>
<h3>Authorities</h3>
<p>No authorization is required.</p>
<br>
<h3>Return Value</h3>
<table cellpadding="5">
<!-- cols="10 90" -->
<tr>
<td align="left" valign="top"><em>value</em></td>
<td align="left" valign="top"><strong>write()</strong> was successful. The
value returned is the number of bytes actually written. This number is less
than or equal to <em>nbyte</em>.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>-1</em></td>
<td align="left" valign="top"><strong>write()</strong> was not successful. The
<em>errno</em> global variable is set to indicate the error.</td>
</tr>
</table>
<br>
<br>
<h3>Error Conditions</h3>
<p>If <strong>write()</strong> is not successful, <em>errno</em> usually
indicates one of the following errors. Under some conditions, <em>errno</em>
could indicate an error other than those listed here.</p>
<table cellpadding="5">
<!-- cols="25 75" -->
<tr>
<th align="left" valign="bottom">Error condition</th>
<th align="left" valign="bottom">Additional information</th>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EACCES">EACCES</a>]</em></td>
<td align="left" valign="top">
<p>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.</p>
<p>If writing to a socket, this error code indicates one of the following:</p>
<ul>
<li>The destination address specified is a broadcast address and the socket
option <samp>SO_BROADCAST</samp> was not set (with a <em>
setsockopt()</em>).</li>
<li>The process does not have the appropriate privileges to the destination
address. This error code can only be returned on a socket with an address
family of <samp>AF_INET</samp> and a type of <samp>SOCK_DGRAM</samp>.</li>
</ul>
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EAGAIN">EAGAIN</a>]</em></td>
<td align="left" valign="top">
<p>If <em>file_descriptor</em> refers to a pipe or FIFO that has its O_NONBLOCK
flag set, this error occurs if the <strong>write()</strong> would have blocked
the calling thread.</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EBADF">EBADF</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EBADFID">EBADFID</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EBUSY">EBUSY</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EDAMAGE">EDAMAGE</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EFAULT">EFAULT</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EFBIG">EFBIG</a>]</em></td>
<td align="left" valign="top">
<p>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, <em>nbyte</em> is greater than 0, and the
starting offset is greater than or equal to 2 GB minus 2 bytes.</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EINTR">EINTR</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EINVAL">EINVAL</a>]</em></td>
<td align="left" valign="top">
<p>For example, the file system that the file resides in does not support large files, and
the starting offset exceeds 2GB minus 2 bytes.</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EIO">EIO</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EJRNDAMAGE">EJRNDAMAGE</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EJRNENTTOOLONG">EJRNENTTOOLONG</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EJRNINACTIVE">EJRNINACTIVE</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EJRNRCVSPC">EJRNRCVSPC</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr><tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENEWJRN">ENEWJRN</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENEWJRNRCV">ENEWJRNRCV</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENOMEM">ENOMEM</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENOSPC">ENOSPC</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENOTAVAIL">ENOTAVAIL</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENOTSAFE">ENOTSAFE</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENXIO">ENXIO</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ERESTART">ERESTART</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ETRUNC">ETRUNC</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ESTALE">ESTALE</a>]</em></td>
<td align="left" valign="top">
<p>If you are accessing a remote file through the Network File System, the file
may have been deleted at the server.</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EUNKNOWN">EUNKNOWN</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
</table>
<p>When the descriptor refers to a socket, <em>errno</em> could indicate one of
the following errors:</p>
<table cellpadding="5">
<!-- cols="25 75" -->
<tr>
<th align="left" valign="bottom">Error condition</th>
<th align="left" valign="bottom">Additional information</th>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ECONNREFUSED">ECONNREFUSED</a>]</em></td>
<td>
<p>This error code can only be returned on sockets that use a connectionless
transport service.</p>
</td></tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EDESTADDRREQ">EDESTADDRREQ</a>]</em></td>
<td align="left" valign="top">
<p>A destination address has not been associated with the socket pointed to by
the <em>fildes</em> parameter. This error code can only be returned on sockets
that use a connectionless transport service.</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EHOSTDOWN">EHOSTDOWN</a>]</em></td>
<td align="left" valign="top">
<p>This error code can only be returned on sockets that use a connectionless
transport service.</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EHOSTUNREACH">EHOSTUNREACH</a>]</em></td>
<td align="left" valign="top">
<p>This error code can only be returned on sockets that use a connectionless
transport service.</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EINTR">EINTR</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EMSGSIZE">EMSGSIZE</a>]</em></td>
<td align="left" valign="top">
<p>The data to be sent could not be sent atomically because the size specified
by <em>nbyte</em> is too large.</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENETDOWN">ENETDOWN</a>]</em></td>
<td align="left" valign="top">
<p>This error code can only be returned on sockets that use a connectionless
transport service.</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENETUNREACH">ENETUNREACH</a>]</em></td>
<td align="left" valign="top">
<p>This error code can only be returned on sockets that use a connectionless
transport service.</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENOBUFS">ENOBUFS</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENOTCONN">ENOTCONN</a>]</em></td>
<td align="left" valign="top">
<p>This error code is returned only on sockets that use a connection-oriented
transport service.</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EPIPE">EPIPE</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EUNATCH">EUNATCH</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EWOULDBLOCK">EWOULDBLOCK</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
</table>
<p>If interaction with a file server is required to access the object, <em>
errno</em> could indicate one of the following errors:</p>
<table cellpadding="5">
<!-- cols="25 75" -->
<tr>
<th align="left" valign="bottom">Error condition</th>
<th align="left" valign="bottom">Additional information</th>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EADDRNOTAVAIL">EADDRNOTAVAIL</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ECONNABORTED">ECONNABORTED</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ECONNREFUSED">ECONNREFUSED</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ECONNRESET">ECONNRESET</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EHOSTDOWN">EHOSTDOWN</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EHOSTUNREACH">EHOSTUNREACH</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENETDOWN">ENETDOWN</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENETRESET">ENETRESET</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENETUNREACH">ENETUNREACH</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ESTALE">ESTALE</a>]</em></td>
<td align="left" valign="top">
<p>If you are accessing a remote file through the Network File System, the file
may have been deleted at the server.</p>
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ETIMEDOUT">ETIMEDOUT</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EUNATCH">EUNATCH</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
</table>
<br>
<h3>Error Messages</h3>
<p>The following messages may be sent from this function:</p>
<table width="100%" cellpadding="5">
<!-- cols="15 85" -->
<tr>
<th align="left" valign="top">Message ID</th>
<th align="left" valign="top">Error Message Text</th>
</tr>
<tr>
<td width="15%" valign="top">CPE3418 E</td>
<td width="85%" valign="top">Possible APAR condition or hardware failure.</td>
</tr>
<tr>
<td align="left" valign="top">CPF3CF2 E</td>
<td align="left" valign="top">Error(s) occurred during running of &amp;1
API.</td>
</tr>
<tr>
<td align="left" valign="top">CPF9872 E</td>
<td align="left" valign="top">Program or service program &amp;1 in library
&amp;2 ended. Reason code &amp;3.</td>
</tr>
<tr>
<td align="left" valign="top">CPFA081 E</td>
<td align="left" valign="top">Unable to set return value or error code.</td>
</tr>
<tr>
<td align="left" valign="top">CPFA0D4 E</td>
<td align="left" valign="top">File system error occurred. Error number
&amp;1.</td>
</tr>
</table>
<br>
<br>
<h3><a name="USAGE_NOTES">Usage Notes</a></h3>
<ol>
<li>This function will fail with error code [ENOTSAFE] when all the following
conditions are true:<br>
<br>
<ul>
<li>Where multiple threads exist in the job.<br>
<br>
</li>
<li>The object on which this function is operating resides in a file system
that is not threadsafe. Only the following file systems are threadsafe for this
function:<br>
<br>
<ul>
<li>"Root" (/)</li>
<li>QOpenSys</li>
<li>User-defined</li>
<li>QNTC</li>
<li>QSYS.LIB</li>
<li>Independent ASP QSYS.LIB</li>
<li>QOPT</li>
<li>Network File System</li>
<li>QFileSvr.400</li>
</ul>
</li>
</ul>
<br>
</li>
<li>QSYS.LIB and independent ASP QSYS.LIB File System Differences
<p>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.</p>
<p>If the file specified is a save file, only complete records will be written
into the save file. A <strong>write()</strong> 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 <strong>write()</strong>'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.</p>
<p>A successful <strong>write()</strong> 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.</p>
<p>You should be careful when writing end-of-file characters in the QSYS.LIB
and independent ASP QSYS.LIB file systems. 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:<br>
<br>
</p>
<ul>
<li>If you previously wrote an end-of-file character as the last character of a
member, do not continue to write data after that end-of-file character.
Continuing to write data will cause a nonsymbolic end-of-file to be written. As
a result, a compile of the member could fail.<br>
<br>
</li>
<li>If you previously wrote an end-of-file character as the last character of a
member, do not write other end-of-file characters preceding it in the file.
This will cause a nonsymbolic end-of-file to be written. As a result, a compile
of the member could fail.<br>
<br>
</li>
<li>If you previously used the integrated file system interface to manipulate a
member that contains an end-of-file character, avoid using other interfaces
(such as the Source Entry Utility or database reads and writes) to manipulate
the member. If you use other interfaces after using the integrated file system
interface, the end-of-file information will be lost.</li>
</ul>
<br>
</li>
<li>QOPT File System Differences
<p>The change and modification times of the file are updated when the file is
closed.</p>
<p>When writing to files on volumes formatted in Universal Disk Format (UDF),
byte locks on the range being written are ignored.</p>
</li>
<li>Network File System Differences
<p>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).</p>
<p>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 <strong>fcntl()</strong> API to get and release these locks.</p>
</li>
<li>QFileSvr.400 File System Differences
<p>The largest buffer size allowed is 16 megabytes. If a larger buffer is
passed, the error EINVAL will be received.</p>
</li>
<li>Sockets Usage Notes<br>
<br>
<ol>
<li><strong>write()</strong> only works with sockets on which a <strong>
connect()</strong> has been issued, since it does not allow the caller to
specify a destination address.<br>
<br>
</li>
<li>To broadcast on an <samp>AF_INET</samp> socket, the socket option <samp>
SO_BROADCAST</samp> must be set (with a <strong>setsockopt()</strong>).<br>
<br>
</li>
<li>When using a connection-oriented transport service, all errors except
<samp>[EUNATCH]</samp> and <samp>[EUNKNOWN]</samp> are mapped to <samp>
[EPIPE]</samp> on an output operation when either of the following occurs:<br>
<br>
<ul>
<li>A connection that is in progress is unsuccessful.</li>
<li>An established connection is broken.</li>
</ul>
<p>To get the actual error, use <strong>getsockopt()</strong> with the <samp>
SO_ERROR</samp> option, or perform an input operation (for example, <strong>
read()</strong>).</p>
</li>
</ol>
</li>
<li>For the file systems that do not support large files, <strong>
write()</strong> will return [EINVAL] if the starting offset exceeds 2GB minus
2 bytes, regardless of how the file was opened. For the file systems that do
support large files, <strong>write()</strong> will return [EFBIG] if the
starting offset exceeds 2GB minus 2 bytes and the file was not opened for large
file access.<br>
<br>
</li>
<li>Using this function successfully on the /dev/null or /dev/zero character
special file results in a return value of the total number of bytes requested
to be written. No data is written to the character special file. In addition,
the change and modification times for the file are updated.<br>
<br>
</li>
<li>If the write exceeds the process soft file size limit, signal SIFXFSZ is
issued.</li>
</ol>
<br>
<h3>Related Information</h3>
<ul>
<li>The &lt;<strong>fcntl.h</strong>&gt; file (see <a href="unix13.htm">Header
Files for UNIX-Type Functions</a>)
</li>
<li>The &lt;<strong>unistd.h</strong>&gt; file (see <a href="unix13.htm">Header
Files for UNIX-Type Functions</a>)
</li>
<li><a href="creat.htm">creat()</a>--Create or Rewrite File
</li>
<li><a href="dup.htm">dup()</a>--Duplicate Open File Descriptor
</li>
<li><a href="dup2.htm">dup2()</a>--Duplicate Open File Descriptor to Another
Descriptor
</li>
<li><a href="fclear.htm">fclear()</a>--Write (Binary Zeros) to Descriptor
</li>
<li><a href="fclear64.htm">fclear64()</a>--Write (Binary Zeros) to Descriptor
(Large File Enabled)</li>
<li><a href="fcntl.htm">fcntl()</a>--Perform File Control Command
</li>
<li><a href="ioctl.htm">ioctl()</a>--Perform I/O Control Request
</li>
<li><a href="lseek.htm">lseek()</a>--Set File Read/Write Offset
</li>
<li><a href="open.htm">open()</a>--Open File
</li>
<li><a href="pread.htm">pread()</a>--Read from Descriptor with Offset
</li>
<li><a href="pread64.htm">pread64()</a>--Read from Descriptor with Offset
(large file enabled)
</li>
<li><a href="pwrite.htm">pwrite()</a>--Write to Descriptor with Offset
</li>
<li><a href="pwrite64.htm">pwrite64()</a>--Write to Descriptor with Offset
(large file enabled)
</li>
<li><a href="read.htm">read()</a>--Read from Descriptor
</li>
<li><a href="readv.htm">readv()</a>--Read from Descriptor Using Multiple
Buffers
</li>
<li><a href="send.htm">send()</a>--Send Data
</li>
<li><a href="sendms.htm">sendmsg()</a>--Send Data or Descriptors or Both
</li>
<li><a href="sendto.htm">sendto()</a>--Send Data
</li>
<li><a href="writev.htm">writev()</a>--Write to Descriptor Using Multiple
Buffers
</li>
</ul>
<br>
<h3>Example</h3>
<p>See <a href="../apiref/aboutapis.htm#codedisclaimer">Code disclaimer information</a>
for information pertaining to code examples.</p>
<p>The following example writes a specific number of bytes to a file:</p>
<pre>
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#define mega_string_len 1000000
main() {
char *mega_string;
int file_descriptor;
int ret;
char fn[]="write.file";
if ((mega_string = (char*) malloc(mega_string_len)) == NULL)
perror("malloc() error");
else if ((file_descriptor = creat(fn, S_IWUSR)) &lt; 0)
perror("creat() error");
else {
memset(mega_string, '0', mega_string_len);
if ((ret = write(file_descriptor, mega_string, mega_string_len)) == -1)
perror("write() error");
else printf("write() wrote %d bytes\n", ret);
if (close(file_descriptor)!= 0)
perror("close() error");
if (unlink(fn)!= 0)
perror("unlink() error");
}
free(mega_string);
}
</pre>
<p><strong>Output:</strong></p>
<pre>
write() wrote 1000000 bytes
</pre>
<br>
<hr>
API introduced: V3R1
<hr>
<center>
<table cellpadding="2" cellspacing="2">
<tr align="center">
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
"unix.htm">UNIX-Type APIs</a> | <a href="aplist.htm">APIs by category</a></td>
</tr>
</table>
</center>
</body>
</html>