将数据写入流。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.

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

返回值Return Value

fwrite 返回实际写入的整个项的数量,如果发生错误,则该数量可能会小于 countfwrite 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. 如果 streambuffer 是空指针,或者如果在 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 函数最多将 count 个项从 size 写入到输出 buffer,每个项的长度为 streamThe fwrite function writes up to count items, of size length each, from buffer to the output stream. stream 相关联的文件指针(如果存在)以实际写入的字节数为增量进行递增。The file pointer associated with stream (if there is one) is incremented by the number of bytes actually written. 如果stream打开在文本模式下,每个换行符替换为回车符-换行符对。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 转换模式下打开 stream 时(例如,通过调用 stream 并使用包含 fopenccs=UNICODEccs=UTF-16LE 的模式参数打开 ccs=UTF-8 时,或者通过使用 _setmode 和包含 _O_WTEXT_O_U16TEXT_O_U8TEXT 的模式参数将该模式更改为 Unicode 转换模式时),会将 buffer 解释为指向包含 UTF-16 数据的 wchar_t 数组的指针。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
fwrite <stdio.h><stdio.h>

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


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

请参阅See Also

流 I/O Stream I/O
_setmode _setmode
fread fread
_fwrite_nolock _fwrite_nolock