将数据写入流。Writes data to a stream.


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


指向要写入的数据的指针。Pointer to data to be written.

项大小(以字节为单位)。Item size, in bytes.

要写入的项的最大数量。Maximum number of items to be written.

指向文件结构的指针。Pointer to FILE structure.

返回值Return Value

fwrite返回实际写入的完整项的数量, 如果发生错误, 则可能小于计数fwrite returns the number of full items actually written, which may be less than count if an error occurs. 此外,如果发生错误,则无法确定文件位置指示器。Also, if an error occurs, the file-position indicator cannot be determined. 如果缓冲区为空指针, 或者如果在 Unicode 模式下指定了要写入的奇数个字节, 则函数将调用无效参数处理程序, 如参数验证中所述。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. 如果允许执行继续, 则此函数会将errno设置为EINVAL , 并返回0。If execution is allowed to continue, this function sets errno to EINVAL and returns 0.


Fwrite函数将每个项的大小缓冲区写入到输出, 并对其进行计算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 is one) is incremented by the number of bytes actually written. 如果在文本模式下打开, 则会将每个换行符替换为回车换行符对。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.

在 Unicode 转换模式下打开时(例如,如果通过调用fopen打开,并使用包含ccs = Unicodeccs = utf-utf-16leccs = utf-8的模式参数),或者如果模式为,则为通过使用 _setmode和包含 _O_WTEXT_O_U16TEXT_O_U8TEXT的模式参数更改为 Unicode 转换模式,缓冲区被解释为指向包含UTF-16 数据。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 is thread-safe. 有关非锁定版本,请参阅 _fwrite_nolockFor a non-locking version, see _fwrite_nolock.


函数Function 必需的标头Required header
fwritefwrite <stdio.h><stdio.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


请参阅 fread 示例。See the example for fread.

请参阅See also

流 I/OStream I/O