<< Previous | Next >>

fwrite (FS2)

int fwrite( File * f, void * buf, int len );

Description

Write data to file opened for writing. The data is written starting at the current position. This is zero (start of file) when it is opened or created, but may be changed by fread(), fwrite(), fshift() or fseek() functions. After writing the data, the current position is advanced to the position just after the last byte written. Thus, sequential calls to fwrite() will add or append data contiguously.

Unlike the previous file system (FILESYSTEM.LIB), this library allows files to be overwritten not just appended. Internally, overwrite and append are different operations with differing performance, depending on the underlying hardware. Generally, appending is more efficient especially with byte-writable flash memory. If the application allows, it is preferable to use append/shift rather than overwrite. In order to ensure that data is appended, use fseek(f, 0, SEEK_END) before calling fwrite().

The same current-position pointer is used for both read and write. If interspersing read and write, then fseek() should be used to ensure the correct position for each operation. Alternatively, the same file can be opened twice, with one descriptor used for read and the other for write. This precludes use of fshift(), since it does not tolerate shared files.

Parameters

f
Pointer to file descriptor (initialized by fopen_wr() or fcreate()).

buf
Data buffer located in root data memory or stack.

len
Length of data (0 to 32767 inclusive).

Return value

 len: Success.
<len: Partial success. Returns amount successfully written. errno gives details.
 0: Failure, or len was zero.

ERRNO values

EBADFD - File descriptor not opened, or is read-only.
EINVAL - len less than zero.
0 - Success, but len was zero.
EIO - I/O error.
ENOSPC - extent out of space.

Library

fs2.LIB

See also

fread (FS2)


Dynamic C Functions << Previous | Next >> rabbit.com