User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







c:stdio.h:fwrite

Table of Contents

fwrite

    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.

C Sourcecode Example

/* 
 * 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

output write into file collection.bin

to read the file use fread

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
c/stdio.h/fwrite.txt · Last modified: 2013/01/22 22:02 (external edit)

Impressum Datenschutz