Write (system Call)
   HOME

TheInfoList



OR:

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 in ...
. 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 interpreted ...
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 have ...
or fd). # The pointer to a
buffer Buffer may refer to: Science * Buffer gas, an inert or nonflammable gas * Buffer solution, a solution used to prevent changes in pH * Buffering agent, the weak acid or base in a buffer solution * Lysis buffer, in cell biology * Metal ion 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. 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 u ...
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.


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 x86 (also known as 80x86 or the 8086 family) is a family of complex instruction set computer (CISC) instruction set architectures initially developed by Intel based on the Intel 8086 microprocessor and its 8088 variant. The 8086 was introd ...
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 Read Read may refer to: * Reading, human cognitive process of decoding symbols in order to construct or derive meaning * Read (automobile), an American car manufactured from 1913 to 1915 * Read (biology), an inferred sequence of base pairs of ...
, 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 unit ...
. 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 The C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header . The functionality descends from a "portable I/O package" written by Mike Lesk at ...
*
getchar The C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header . The functionality descends from a "portable I/O package" written by Mike Lesk at ...
*
fprintf The C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header . The functionality descends from a "portable I/O package" written by Mike Lesk at ...
*
read (system call) In modern POSIX compliant operating systems, a program that needs to access data from a file stored in a file system uses the read system call. The file is identified by a file descriptor that is normally obtained from a previous call to open. Thi ...
*
sync (Unix) sync is a standard system call in the Unix operating system, which commits all data in the kernel filesystem to non-volatile storage buffers, i.e., data which has been scheduled for writing via low-level I/O system calls. Higher-level I/O layers ...


References


External links


POSIX write
* {{Computer files C POSIX library System calls