Home

fwrite

fwrite is a standard C library function used to write blocks of data from a memory buffer to a file stream. It is declared in stdio.h and is commonly used for writing binary data or large arrays of objects, as opposed to formatting functions like fprintf or snprintf.

Prototype and parameters: size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); The function writes

Return value and error handling: If successful, fwrite returns nmemb. If an error occurs or the end

Notes and usage: fwrite writes to the stream’s internal buffer, and data may not be physically written

nmemb
items,
each
of
size
bytes,
from
the
buffer
pointed
to
by
ptr
to
the
given
stream.
The
number
of
items
successfully
written
is
returned.
The
total
number
of
bytes
written
is
size
*
returned_nmemb.
of
the
file
is
reached
for
the
stream,
it
may
return
a
smaller
value
than
nmemb.
To
determine
whether
writing
failed,
test
the
return
value
and,
if
needed,
use
ferror(stream)
to
check
for
an
error
condition.
The
feof
function
is
generally
used
for
input
streams;
its
meaning
on
output
streams
is
implementation-defined.
to
the
underlying
medium
until
the
buffer
is
flushed.
To
ensure
durability,
call
fflush(stream)
or
close
the
stream
with
fclose.
In
text
mode
on
some
platforms,
newline
translation
may
occur;
for
portable
binary
data,
open
the
file
in
binary
mode.
In
C++,
fwrite
is
available
via
std::fwrite
in
<cstdio>,
integrating
with
C++
streams
when
interoperation
with
C
code
is
needed.