FREAD(3S) SysV FREAD(3S)
NAME
fread, fwrite - binary input/output
SYNOPSIS
#include <stdio.h>
size_t fread(ptr, size, nmemb, stream)
void *ptr;
size_t size;
size_t nmemb;
FILE *stream;
size_t fwrite(ptr, size, nmemb, stream)
const void *ptr;
size_t size;
size_t nmemb;
FILE *stream;
DESCRIPTION
The fread function reads, into the array pointed to by ptr, up to nmemb
elements whose size is specified by size, from the stream pointed to by
stream. The file position indicator for the stream (if defined) is
advanced by the number of characters successfully read. If an error
occurs, the resulting value of the file position indicator for the stream
is indeterminate. If a partial element is read, its value is
indeterminate.
If stream is stdin or is unbuffered or line-buffered, then all active
line-buffered output streams are flushed before any call to read(2) to
satisfy the fread.
fread returns the number of elements successfully read, which may be less
than nmemb if a read error or EOF is encountered. If size or nmemb is
zero, fread returns zero and the contents of the array and the state of
the stream remain unchanged. Otherwise, if a write error occurs, the
error indicator for the stream is set and errno is set to indicate the
error.
The st_atime field is marked for update by the first successful execution
of fread using stream that returns data not supplied by a prior call to
ungetc.
The fwrite function writes, from the array pointed to by ptr, up to nmemb
elements whose size is specified by size, to the stream pointed to by
stream. The file position indicator for the stream (if defined) is
advanced by the number of characters successfully written. If an error
occurs, the resulting value of the file position indicator for the stream
is indeterminate.
fwrite returns the number of items actually written, which will be less
than nmemb only if a write error is encountered.
If size or nmemb is zero, fwrite returns zero and the contents of the
array and the state of the stream remain unchanged. Otherwise, if a write
error occurs, the error indicator for the stream is set and errno is set
to indicate the error.
The st_ctime and st_mtime fields of the file are marked for update
between the successful execution of fwrite and the next successful
completion of a call to fflush(3S) or fclose(3S) on the same stream or a
call to exit(2) or abort(3C).
ERRORS
fread fails if either stream is unbuffered, or the data needs to be read
into the stream's buffer and the call to fread caused an underlying
read(2) or lseek(2) to be invoked, and:
[EAGAIN] The O_NONBLOCK flag is set for the file descriptor
underlying stream and the process would be delayed in the
write operation.
[EBADF] The file descriptor underlying stream is not a valid file
descriptor open for reading.
[EINTR] The read operation was interrupted by a signal which was
caught, and no data was transferred.
[EIO] 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 SIGTTOU
and the process group of the process is orphaned.
fwrite fails if either stream is unbuffered, or the buffer needed to be
flushed and the call to fwrite caused an underlying read or lseek to be
invoked, and:
[EAGAIN] The O_NONBLOCK flag is set for the file descriptor
underlying stream and the process would be delayed in the
write operation.
[EBADF] The file descriptor underlying stream is not a valid file
descriptor open for writing.
[EFBIG] An attempt was made to write to a file that exceeds the
process' file size limit or the maximum file size.
[EINTR] The write operation was interrupted by a signal which was
caught, and no data was transferred.
[EIO] 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 SIGTTOU
and the process group of the process is orphaned.
[ENOSPC] There was no free space remaining on the device containing
the file.
[EPIPE] An attempt was made to write to a pipe or FIFO that is not
open for reading by any process. A SIGPIPE signal will
also be sent to the process.
NOTES
If the fwrite call indicates that less data was written than had been
requested, an underlying error has ocrurred and errno is set.
To be compatible with traditional C usage, make fread and fwrite return
an int by compiling your program with the
-A nansi
option (see cc(1)) and inserting the directive
#define _CLASSIC_TYPES
before any
#include
directives.
SEE ALSO
cc(1), read(2), write(2), fopen(3S), getc(3S), gets(3S), printf(3S),
putc(3S), puts(3S), scanf(3S), stdio(3S).
DIAGNOSTICS
fread and fwrite return the number of items read or written. If nitems
is non-positive, no characters are read or written and 0 is returned by
both fread and fwrite. Zero is returned on EOF or error.