【お知らせ】この部分は英語原文のみでの提供となります。何卒ご了承ください。
2.1.9.15 fwrite
Contents
Description
Writes up to count items, of size length each, from buffer to the output 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
Return
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.
Examples
EX1
//The following opens a file named fwrite.out and writes 25 characters to the file. //It then tries to open fwrite.out and read in 25 characters.If the attempt //succeeds,the program displays the number of actual items read. void test_fwrite() { FILE *stream; char list[30]; int i, numread, numwritten; // Open file in text mode: stream = fopen( "fwrite.out", "w+t" ); if(stream != NULL ) { for ( i = 0; i < 25; i++ ) list[i] = (char)('z' - i); // Write 25 characters to stream numwritten = fwrite( list, sizeof( char ), 25, stream ); printf( "Wrote %d items\n", numwritten ); fclose( stream ); } else printf( "Problem opening the file\n" ); stream = fopen( "fwrite.out", "r+t" ); if(stream != NULL ) { // Attempt to read in 25 characters numread = fread( list, sizeof( char ), 25, stream ); printf( "Number of items read = %d\n", numread ); printf( "Contents of buffer = %.25s\n", list ); fclose( stream ); } else printf( "File could not be opened\n" ); }
Remark
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 carriage return is replaced with a carriage-return - linefeed pair. The replacement has no effect on the return value.
See Also
Header to Include
origin.h