fread(3S) fread(3S)
NAME
fread, fwrite - binary input/output
SYNOPSIS
#include <stdio.h>
sizet fread(void *ptr, sizet size, sizet nitems, FILE *stream);
sizet fwrite(const void *ptr, sizet size, sizet nitems, FILE *stream);
DESCRIPTION
fread() reads into an array pointed to by ptr up to nitems items of
data from stream, where an item of data is a sequence of bytes of
length size. The sequence is not necessarily terminated by a null
byte. fread() stops reading bytes if an end-of-file or error condition
is encountered while reading stream, or if nitems items have been
read. fread() increments the data pointer in stream to point to the
byte following the last byte read if there is one. fread() does not
change the contents of stream. fread() returns the number of items
read. The return value is less than nitems only if a read error or an
EOF occurs.
fwrite() writes to the named output stream at most nitems items of
data from the array pointed to by ptr, where an item of data is a
sequence of bytes of length size. This sequence is not necessarily
terminated by a null byte. fwrite() stops writing when it has written
nitems items of data or if an error condition is encountered on
stream. fwrite() does not change the contents of the array pointed to
by ptr. fwrite() increments the data-pointer in stream by the number
of bytes written. fwrite() returns the number of items written.
If size or nitems is zero, then fread() and fwrite() return a value of
0. The contents of the array pointed to by ptr and the state of stream
remain unchanged.
The ferror() or feof() routines must be used to distinguish between an
error condition and end-of-file condition.
ERRORS
The following error code descriptions are function-specific. You will
find a general description in introprm2(2) or in errno(5).
The fread() function will fail if:
EAGAIN The ONONBLOCK flag is set for the file descriptor under-
lying stream, and the process is delayed.
EBADF The file descriptor underlying stream is not a valid file
descriptor opened for reading.
EINTR The read operation was terminated due to the receipt of a
signal; no data was transferred.
Page 1 Reliant UNIX 5.44 Printed 11/98
fread(3S) fread(3S)
EIO A physical I/O error has occurred, or the process is in a
background process group attempting to read from its con-
trolling terminal, and either the process is ignoring or
blocking the SIGTTIN signal or the process group is
orphaned. This error may also be generated for implementa-
tion-dependent reasons.
EOVERFLOW The file is a regular file and an attempt was made to read
at or beyond the offset maximum associated with the corre-
sponding stream.
The fread() function may fail if:
ENOMEM Insufficient storage space is available.
ENXIO A request was made of a non-existent device, or the
request was outside the capabilities of the device.
The fwrite() function will fail if:
EAGAIN The ONONBLOCK flag is set for the file descriptor under-
lying stream and the process would be delayed in the write
operation.
EBADF The file descriptor underlying stream is not a valid
descriptor open for writing.
EFBIG An attempt was made to write to a file that exceeds the
maximum file size or the process file size limit [see
ulimit(2)].
EFBIG The file is a regular file and an attempt was made to
write at or beyond the offset maximum associated with the
corresponding stream.
EINTR The write operation was terminated due to the receipt of a
signal, and no data was transferred.
EIO A physical I/O error has occurred, or the process is a
member of a background process group attempting to write
to its controlling terminal, TOSTOP is set, the process is
neither ignoring nor blocking SIGTOU and the process group
of the process is orphaned. This error may also be
returned under implementation-dependent conditions.
ENOSPC There was no free space remaining on the device containing
the file.
EPIPE An attempt is made to write a pipe or FIFO that is not
open for reading by any process. A SIGPIPE signal will
also be sent to the process.
Page 2 Reliant UNIX 5.44 Printed 11/98
fread(3S) fread(3S)
The fwrite() function may fail if:
ENOMEM Insufficient storage space is available.
ENXIO A request was made of a non-existent device, or the
request was outside the capabilities of the device.
RESULT
If an error occurs, the error indicator for stream is set.
SEE ALSO
exit(2), lseek(2), read(2), write(2), abort(3C), fclose(3S),
fopen(3S), getc(3S), gets(3S), printf(3S), putc(3S), puts(3S),
scanf(3S), stdio(3S), lfs(5), stdio(5).
Page 3 Reliant UNIX 5.44 Printed 11/98