The write is one of the most basic
routines provided by a
Unix-like
A Unix-like (sometimes referred to as UN*X 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 Unix-li ...
operating system kernel
The kernel is a computer program at the core of a computer's operating system and generally has complete control over everything in the system. It is the portion of the operating system code that is always resident in memory and facilitates ...
. 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
In the pursuit of knowledge, data (; ) is a collection of discrete values that convey information, describing quantity, quality, fact, statistics, other basic units of meaning, or simply sequences of symbols that may be further interpret ...
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 ha ...
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 fd, const void *buf, size_t nbytes);
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
In the C programming language, data types constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables. Data types also determin ...
. Note that
write()
The write is one of the most basic routines provided by a Unix-like operating system 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 ...
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
An error (from the Latin ''error'', meaning "wandering") is an action which is inaccurate or incorrect. In some usages, an error is synonymous with a mistake. The etymology derives from the Latin term 'errare', meaning 'to stray'.
In statistics ...
below.
Usage example
#include
#include
#include
#include
#include
int main (int argc, char *argv[])
Errors encountered during operation
Listed below are some errors that could be encountered during writing to a file. The errors are macros listed in errno.h.
Higher level I/O functions calling write
The write system call is not an ordinary function, in spite of the close resemblance. For example, in Linux with the
x86 architecture, the system call uses the instruction
INT 80H, in order to transfer control over to the kernel. The write system call, and its counterpart
read, being low level functions, are only capable of understanding
bytes
The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable uni ...
. Write cannot be used to write records, like
classes. Thus, higher level input-output functions (like
printf
The printf format string is a control parameter used by a class of functions in the input/output libraries of C and many other programming languages. The string is written in a simple template language: characters are usually copied literal ...
) are required. Often, the high-level interface is preferred, as compared to the cluttered low-level interface. These functions call other functions internally, and these in turn can make calls to write, giving rise to a layered assembly of functions.
[http://ftp.gnu.org/gnu/glibc/ GNU C Library Download]
With the use of this assembly the higher level functions can collect bytes of data and then write the required data into a file.
See also
*
fwrite
*
getchar
*
fprintf
*
read (system call)
*
sync (Unix)
References
External links
POSIX write*
{{Computer files
C POSIX library
System calls