| Types and objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <stdio.h> | ||
| (until C99) | ||
| (since C99) | ||
Writescount of objects from the given arraybuffer to the output streamstream. The objects are written as if by reinterpreting each object as an array ofunsignedchar and callingfputcsize times for each object to write thoseunsignedchars intostream, in order. The file position indicator for the stream is advanced by the number of characters written.
If an error occurs, the resulting value of the file position indicator for the stream is indeterminate.
Contents |
| buffer | - | pointer to the first object in the array to be written |
| size | - | size of each object |
| count | - | the number of the objects to be written |
| stream | - | pointer to the output stream |
The number of objects written successfully, which may be less thancount if an error occurs.
Ifsize orcount is zero,fwrite returns zero and performs no other action.
#include <assert.h>#include <stdio.h>#include <stdlib.h> enum{ SIZE=5}; int main(void){double a[SIZE]={1,2,3,4,5};FILE* f1=fopen("file.bin","wb");assert(f1);size_t r1= fwrite(a,sizeof a[0], SIZE, f1);printf("wrote %zu elements out of %d requested\n", r1, SIZE);fclose(f1); double b[SIZE];FILE* f2=fopen("file.bin","rb");size_t r2=fread(b,sizeof b[0], SIZE, f2);fclose(f2);printf("read back: ");for(size_t i=0; i< r2;++i)printf("%0.2f ", b[i]);}
Output:
wrote 5 elements out of 5 requestedread back: 1.00 2.00 3.00 4.00 5.00
(C99)(C11)(C11)(C11)(C11) | prints formatted output tostdout, a file stream or a buffer (function)[edit] |
| writes a character string to a file stream (function)[edit] | |
| reads from a file (function)[edit] | |
C++ documentation forfwrite | |