Programming Reference/Librarys
Question & Answer
Q&A is closed
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
The fwrite() function writes, from the array buffer, count objects of size size to stream. The return value is the number of objects written.
/* * fwrite example code * http://code-reference.com/c/stdio.h/fwrite */ #include <stdio.h> /* including standard library */ //#include <windows.h> /* uncomment this for Windows */ struct CD_COLLECTION { char album_name[42]; int year; char artist[42]; }; int main (void) { FILE *stream = fopen ("collection.bin","wb"); struct CD_COLLECTION collection[5]; unsigned short i = 0; for (i=0; i < 5 ; i++) { sprintf(collection[i].album_name, "Interpret %i", i); collection[i].year=2012; sprintf(collection[i].artist,"Mr Unrockable %i", i); } fwrite (collection, sizeof(struct CD_COLLECTION), 5, stream); fclose (stream); return 0; }
output write into file collection.bin
to read the file use fread