The fwrite()
function writes binary and text data from an array to a given data stream. It is available in both C and C++.
std::size_t fwrite( const void* buffer, std::size_t size, std::size_t count, std::FILE* stream );
buffer
: points to the first object in the array to be writtensize
: the item size in bytescount
: the number of the objects to be writtenstream
: points to a FILE object that specifies an output streamThe function returns the total number of elements that were successfully written. If this number is less than the count it signifies that an error has prevented all the characters from being written.
The following code shows how to write to a file using fwrite()
:
#include <cstdio>#include <iostream>using namespace std;int main(){int retVal;FILE *fp;char buffer[] = "Example text";fp = fopen("file.txt","w");int returns = fwrite(buffer,sizeof(buffer),1,fp);cout << "fwrite returned " << returns;return 0;}