FAT | RFS | XFS

fwrite()

PROTOTYPE

#include <stdio.h>

size_t fwrite(const void *buf, size_t size, size_t nitems, FILE *file);

DESCRIPTION

fwrite() writes data in the buffer specified by buf to the open regular file identified by file at its current position, or end if open in append mode, until either nitems elements are written or an error occurs. size is the number of bytes in a single element.

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 size or nitems are 0, fwrite() returns 0. Otherwise, it returns the number of elements successfully written (which may be 0). If any data is written, it updates the file’s status change and data modification timestamps. If an error occurs, it sets errno and the error flag.

ERROR CODES

EBADF file is not the handle of a file open in write or append mode.
EFAULT buf equals NULL.
ENOSPC No space is left on volume to write file data.
ENXIO The volume has been removed.
EROFS The volume is read-only.

EXAMPLE

FILE *file;

/*-------------------------------------------------------------------*/
/* Open file for writing.                                            */
/*-------------------------------------------------------------------*/
file = fopen("/flash/a/b/file1.txt", "w");
if (file == NULL)
  return;

/*-------------------------------------------------------------------*/
/* Write greeting to stream.                                         */
/*-------------------------------------------------------------------*/
if (fwrite("Hello world!\n", 1, 13, file) != 13)
  return -1;