User Tools

Site Tools


c:stdio.h:fwrite

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

c:stdio.h:fwrite [2013/01/22 22:02] (current)
Line 1: Line 1:
 +{{keywords>wiki library source code example reference}}
 +====== fwrite ======
 +
 +<code c>
 +    size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
 +</code>
 +The fwrite() function writes, from the array buffer, count objects of size size to stream. The return value is the number of objects written. 
 +
 +===== C Sourcecode Example =====
 +<code c>
 +/* 
 + * 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;
 +}
 +
 +</code>
 +==== output ====
 +output write into file **collection.bin**
 +
 +to read the file use [[c:stdio.h:fread|]]
  
advertising
c/stdio.h/fwrite.txt · Last modified: 2013/01/22 22:02 (external edit)

Impressum Datenschutz