RAD Studio
ContentsIndex
PreviousUpNext
fwrite

Header File 

stdio.h  

Category 

Input/output Routines 

Prototype 

size_t fwrite(const void *ptr, size_t size, size_t n, FILE *stream); 

Description 

Writes to a stream. 

fwrite appends n items of data each of length size bytes to the given output file. The data written begins at ptr. The total number of bytes written is (n x size). ptr in the declarations is a pointer to any object. 

Return Value 

On successful completion fwrite returns the number of items (not bytes) actually written.  

On error it returns a short count. 

Example  

#include <stdio.h>
struct mystruct
{
  int i;
  char ch;
};
int main(void)
{
   FILE *stream;
   struct mystruct s;
   if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open file TEST.$$$ */
   {
      fprintf(stderr, "Cannot open output file.\n");
      return 1;
   }
   s.i = 0;
   s.ch = 'A';
   fwrite(&s, sizeof(s), 1, stream); /* write struct s to file */
   fclose(stream); /* close file */
   return 0;
}

Portability

POSIX 
Win32 
ANSI C 
ANSI C++ 
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!