FAT | RFS | XFS

write()

PROTOTYPE

#include <posix.h>

ssize_t write(int fid, const void *buf, size_t bcnt);

DESCRIPTION

write() copies user data from the buffer specified by buf to the open regular file identified by fid at its current position, or end if open in append mode, until bcnt bytes are written or an error occurs.

If the initial file position is past end-of-file, due to a prior seek, the gap between the initial end and the initial position is filled with zeros. The file position is advanced by the number of bytes written.

If bcnt is 0, write() returns 0. Otherwise, it returns the number of bytes written or -1 if nothing was written. If an error occurs, errno and the error flag are set. Successful write() calls update the file’s status change and data modification timestamps.

ERROR CODES

EBADF fid is not the descriptor of a file open in write or append mode.
EFAULT buf equals NULL.
ENOSPC The volume is full.
ENXIO The associated volume has been removed.
EROFS The volume is read-only.

EXAMPLE

int fid;

/*-------------------------------------------------------------------*/
/* Open a text file in write only mode.                              */
/*-------------------------------------------------------------------*/
fid = open("/ram_fs/a/b/file1.txt", O_WRONLY);

/*-------------------------------------------------------------------*/
/* Write the contents of str1 to the file.                           */
/*-------------------------------------------------------------------*/
if (write(fid, "Hello World", 11) != 11)
  return -1;