将数据写入流。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. 如果任一缓冲区是 null 指针,或如果在 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. 如果允许执行继续,此函数将errnoEINVAL并返回 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 linefeed is replaced with a carriage-return - linefeed pair. 该替换不会影响返回值。The replacement has no effect on the return value.

在 Unicode 转换模式下打开 — 例如,如果打开通过调用fopen和使用模式参数,其中包含ccs= UNICODEccs = UTF 16LE,或ccs = utf-8,或如果模式更改为 Unicode 转换模式使用 _setmode和模式参数,其中包含 _O_WTEXT_O_U16TEXT,或 _O_U8TEXT-缓冲区解释为指向的指针数组wchar_t包含 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