fwrite
size_t fwrite ( const void * buffer, size_t size, size_t count, FILE * stream ); | stdio.h |
cplusplus.com |
Write a block of data to a stream.
Writes count number of items, each one with a size of
size bytes, from the memory block pointed by buffer
to the current position in the stream.
Stream's postion indicator is increased by the number of bytes written.
On files opened in text mode some translations may occur with carriage-return and line-feed
characters.
The total number of bytes to be written is (size x count).
Normally the parameter size should contain the size of each item
(char, int, long, structures ...) to be written and count the number of these
items. But this is not absolute, you can specify any combination
of numbers which result of (size x count) match the size
in bytes of the block to be written.
Parameters.
Return Value.
Number of full items (not bytes) successfully written. This may be less than
the specified in count parameter if an error occurred.
Portability.
Defined in ANSI-C.
Example.
/* fwrite example : write buffer */
#include <stdio.h>
main ()
{
FILE * pFile;
char buffer[] = "This buffer contains 34 characters";
pFile = fopen ("myfile.txt" , "w");
fwrite (buffer , 1 , 34 , pFile);
fclose (pFile);
return 0;
}
A file called myfile.txt is created and the content of a 34
characters buffer is written to it. The content of the file will be:
This buffer contains 34 characters
See also.
fread,
fscanf,
getc
fgetc