write
Writes to a file.
Defined in header <io.h>
Syntax
#include <io.h>
int write(int handle, void *buf, unsigned len);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ | ■ |
Remarks
write writes a buffer of data to the file or device named by the given handle, handle is a file handle obtained from a creat, open, dup, or dup2 call.
This function attempts to write len bytes from the buffer pointed to by buf to the file associated with handle. Except when write is used to write to a text file, the number of bytes written to the file will be no more than the number requested.
| The maximum | number of bytes that write can write is 65,534, |
because 65,535 (0xFFFF) is the same as -1, which is the error return indicator for write. On text files, when write sees a linefeed (LF) character, it outputs a CR/LF pair.
If the number of bytes actually written is less than that requested, the condition should be considered an error and probably indicates a full disk. For disks or disk files, writing, always proceeds from the current file pointer. For devices, bytes are sent directly to the device. For files opened with the OAPPEND option, the file pointer is positioned to EOF by write before writing the data.
Return value
write returns the number of bytes written. A write to a text file does not count generated carriage returns. In case of error, write returns -1 and sets the global variable errno to one of the following:
| EACCES | Permission denied |
| EBADF | Bad file number |
See also
creatlseekopenreadwriteAGlobalvariablesBorlandC++providesyouwithpredefinedglobalvariablesformanycommonneedssuchasdatestimescommand-lineargumentssoonThischapterdefinesdescribesthem
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/io.h/write/
· short form /3.1/write/