Home

fread

fread is a standard C library function that reads data from a file stream into memory. It is declared in stdio.h as size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream).

The function reads up to nmemb elements, each of size bytes, from the stream into the array

fread is binary-safe in the sense that it reads raw bytes without interpreting them. However, on platforms

To distinguish between end-of-file and error, use feof and ferror. On error, errno may be set. fread

pointed
to
by
ptr.
It
returns
the
number
of
elements
successfully
read,
which
may
be
less
than
nmemb
if
end-of-file
is
encountered
or
if
a
read
error
occurs.
If
size
or
nmemb
is
zero,
the
call
returns
0.
The
actual
number
of
bytes
read
is
the
return
value
multiplied
by
size.
that
support
text
mode
files,
translation
of
newline
characters
can
occur
when
a
stream
is
opened
in
text
mode,
which
can
affect
the
number
of
bytes
read.
The
FILE*
object
provides
buffering,
so
fread
may
fetch
data
from
the
underlying
source
in
larger
chunks
than
requested.
is
intended
for
bulk,
unformatted
input
and
is
commonly
used
to
read
binary
data
or
large
blocks
of
data,
as
opposed
to
formatted
input
functions
such
as
fscanf
or
line-oriented
input
such
as
fgets.
A
typical
usage
pattern
is
to
repeatedly
call
fread
with
a
fixed-size
buffer
and
process
the
portion
actually
read
until
it
returns
0,
then
check
for
errors
or
end-of-file.