Edit

Share via


fwrite

Writes data to a stream.

Syntax

size_t fwrite(
   const void *buffer,
   size_t size,
   size_t count,
   FILE *stream
);

Parameters

buffer
Pointer to data to be written.

size
Item size, in bytes.

count
Maximum number of items to be written.

stream
Pointer to FILE structure.

Return value

fwrite returns the number of full items the function writes, which may be less than count if an error occurs. Also, if an error occurs, the file-position indicator can't be determined. If either stream or buffer is a null pointer, or if an odd number of bytes to be written is specified in Unicode mode, the function invokes the invalid parameter handler, as described in Parameter validation. If execution is allowed to continue, this function sets errno to EINVAL and returns 0.

Remarks

The fwrite function writes up to count items, of size length each, from buffer to the output stream. The file pointer associated with stream (if there's one) is incremented by the number of bytes fwrite writes. If stream is opened in text mode, each line feed is replaced with a carriage return-line feed pair. The replacement has no effect on the return value.

When stream is opened in Unicode translation mode—for example, if stream is opened by calling fopen and using a mode parameter that includes ccs=UNICODE, ccs=UTF-16LE, or ccs=UTF-8, or if the mode is changed to a Unicode translation mode by using _setmode and a mode parameter that includes _O_WTEXT, _O_U16TEXT, or _O_U8TEXTbuffer is interpreted as a pointer to an array of wchar_t that contains UTF-16 data. An attempt to write an odd number of bytes in this mode causes a parameter validation error.

Because this function locks the calling thread, it's thread-safe. For a non-locking version, see _fwrite_nolock.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Requirements

Function Required header
fwrite <stdio.h>

For more compatibility information, see Compatibility.

Example

See the example for fread.

See also

Stream I/O
_setmode
fread
_fwrite_nolock
_write