The write is one of the most basic
routines provided by a
Unix-like
A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
operating system
An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs.
Time-sharing operating systems scheduler (computing), schedule tasks for ...
kernel. It writes data from a buffer declared by the user to a given device, such as a file. This is the primary way to output data from a program by directly using a system call. The destination is identified by a
numeric code. The
data
Data ( , ) are a collection of discrete or continuous values that convey information, describing the quantity, quality, fact, statistics, other basic units of meaning, or simply sequences of symbols that may be further interpreted for ...
to be written, for instance a piece of text, is defined by a
pointer and a size, given in number of bytes.
write
thus takes three arguments:
# The file code (
file descriptor
In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket.
File descriptors typically h ...
or fd).
# The pointer to a
buffer where the data is stored (buf).
# The number of bytes to write from the buffer (nbytes).
POSIX usage
The write call interface is standardized by the POSIX specification. Data is written to a file by calling the write function. The function prototype is:
ssize_t write(int fildes, const void *buf, size_t nbyte);
In above syntax,
ssize_t
is a
typedef
typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (''alias'') for another data type, but does not create a new type, except in the obscure case of a qualified typedef of ...
. It is a signed data type defined in
stddef.h
. Note that
write()
does not return an unsigned value; it returns -1 if an error occurs so it must return a signed value.
The write function returns the number of bytes successfully written into the file, which may at times be less than the specified nbytes. It returns -1 if an exceptional condition is encountered, see section on
errors below.
Linux
On Linux, write is system call number 1.
See also
*
fwrite
*
getchar
The C (programming language), C programming language provides many standard library subroutine, functions for computer file, file input/output, input and output. These functions make up the bulk of the C standard library header file, header . The ...
*
fprintf
*
read (system call)
*
sync (Unix)
References
External links
POSIX write*
{{Computer files
C POSIX library
System calls