Write bytes to a file
#include <unistd.h> ssize_t write( int fildes, const void* buf, size_t nbyte );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The write() function attempts to write nbyte bytes to the file associated with the open file descriptor, fildes, from the buffer pointed to by buf.
If nbyte is zero, write() returns zero, and has no other effect.
On a regular file or other file capable of seeking, and if O_APPEND isn't set, write() starts at a position in the file given by the file offset associated with fildes. If O_APPEND is set, the file offset is set to the end of file before each write operation. Before successfully returning from write(), the file offset is incremented by the number of bytes actually written. On a regular file, if this incremented file offset is greater than the length of the file, the length of the file is set to this file offset.
On a file not capable of seeking, write() starts at the current position.
If write() requests that more bytes be written than there's room for (for example, all blocks on a disk are already allocated), only as many bytes as there's room for are written. For example, if there's only room for 80 more bytes in a file, a write of 512 bytes would return 80. The next write of a nonzero number of bytes would give a failure return (except as noted below).
When write() returns successfully, its return value is the number of bytes actually written to the file. This number is never greater then nbyte, although it may be less than nbyte under certain circumstances detailed below.
If write() is interrupted by a signal before it has written any data, it returns a value of -1, and errno is set to EINTR. However, if write() is interrupted by a signal after it has successfully written some data, it returns the number of bytes written.
If the value of nbyte is greater than INT_MAX, write() returns -1 and sets errno to EINVAL. See <limits.h>.
Write requests to a pipe (or FIFO) are handled the same as a regular file, with the following exceptions:
If you call write() with nbyte greater than PIPE_BUF bytes, it either transfers what it can and returns the number of bytes written, or transfers no data, returning -1 and setting errno to EAGAIN. Also, if nbyte is greater than PIPE_BUF bytes and all data previously written to the pipe has been read (that is, the pipe is empty), write() transfers at least PIPE_BUF bytes.
When attempting to write to a file (other than a pipe or FIFO) that supports nonblocking writes and can't accept the data immediately:
If write() is called with the file offset beyond the end-of-file, the file is extended to the current file offset with the intervening bytes filled with zeroes. This is a useful technique for pregrowing a file.
If write() succeeds, the st_ctime and st_mtime fields of the file are marked for update.
The number of bytes written, or -1 if an error occurred (errno is set).
#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> char buffer[] = { "A text record to be written" }; int main( void ) { int fd; int size_written; /* open a file for output */ /* replace existing file if it exists */ fd = creat( "myfile.dat", S_IRUSR | S_IWUSR ); /* write the text */ size_written = write( fd, buffer, sizeof( buffer ) ); /* test for error */ if( size_written != sizeof( buffer ) ) { perror( "Error writing myfile.dat" ); return EXIT_FAILURE; } /* close the file */ close( fd ); return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
close(), creat(), dup(), dup2(), errno, fcntl(), lseek(), open(), pipe(), read(), readv(), select(), writev()