Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define _POSIX_SOURCE
#include <unistd.h>
ssize_t write(int fs, const void *buf, size_t N);
#define _XOPEN_SOURCE_EXTENDED 1
#include <unistd.h>
ssize_t write(int fs, const void *buf, ssize_t N);
#define _OE_SOCKETS
#include <unistd.h>
ssize_t write(int fs, const void *buf, ssize_t N);
Writes N bytes from buf to the file or socket associated with fs. N should not be greater than INT_MAX (defined in the limits.h header file). If N is zero, write() simply returns 0 without attempting any other action.
If fs refers to a regular file or any other type of file on which a process can seek, write() begins writing at the file offset associated with fs. A successful write() 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.
If fs refers to a file on which a process cannot seek, write() begins writing at the current position. There is no file offset associated with such a file.
If O_APPEND (defined in the fcntl.h header file) is set for the file, write() sets the file offset to the end of the file before writing the output.
If there is not enough room to write the requested number of bytes (for example, because there is not enough room on the disk), write() outputs as many bytes as the remaining space can hold.
A successful write() updates the change and modification times for the file.
If fs refers to a socket, write() is equivalent to send() with no flags set.
If there is not enough available buffer space to hold the socket data to be transmitted, and the socket is in blocking mode, write() blocks the caller until additional buffer space becomes available. If the socket is in nonblocking mode, write() returns -1 and sets the error code to EWOULDBLOCK. See fcntl() — Control open file descriptors or ioctl() — Control device for a description of how to set the nonblocking mode.
For datagram sockets, this call sends the entire datagram, provided that the datagram fits into the TCP/IP buffers. Stream sockets act like streams of information with no boundaries separating data. For example, if an application program wishes to send 1000 bytes, each call to this function can send 1 byte or 10 bytes or the entire 1000 bytes. Therefore, application programs using stream sockets should place this call in a loop, calling this function until all data has been sent.
Special behavior for C++ and sockets: To use this function with C++, you must use the _XOPEN_SOURCE_EXTENDED 1 feature test macro.
Large file support for z/OS UNIX files: Large z/OS UNIX files are supported automatically for AMODE 64 C/C++ applications. AMODE 31 C/C++ applications must be compiled with the option LANGLVL(LONGLONG) and define the _LARGE_FILES feature test macro before any headers are included to enable this function to operate on z/OS UNIX files that are larger than 2 GB in size. File size and offset fields are enlarged to 63 bits in width. Therefore, any other function operating on the file is required to define the _LARGE_FILES feature test macro as well.
If successful, write() returns the number of bytes actually written, less than or equal to N.
A value of 0 or greater indicates the number of bytes sent. However, this does not assure that data delivery was complete. A connection can be dropped by a peer socket and a SIGPIPE signal generated at a later time if data delivery is not complete.
An attempt was made to write a file that exceeds the system established maximum file size or the process's file size limit.
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 fields.
A write to a STREAMS file may fail if an error message has been received at the STREAM head. In this case, errno is set to the value included in the error message.
⁄* CELEBW35
This example writes a certain amount of bytes to a file, using write().
*⁄
#define _POSIX_SOURCE
#include <fcntl.h>
#include <sys⁄stat.h>
#include <sys⁄types.h>
#include <unistd.h>
#undef _POSIX_SOURCE
#include <stdlib.h>
#include <stdio.h>
#define mega_string_len 1000000
main() {
char *mega_string;
int fd, ret;
char fn[]="write.file";
if ((mega_string = (char*) malloc(mega_string_len)) == NULL)
perror("malloc() error");
else if ((fd = creat(fn, S_IWUSR)) < 0)
perror("creat() error");
else {
memset(mega_string, '0', mega_string_len);
if ((ret = write(fd, mega_string, mega_string_len)) == -1)
perror("write() error");
else printf("write() wrote %d bytes\n", ret);
close(fd);
unlink(fn);
}
}
write() wrote 1000000 bytes