int  putw ( int integer , FILE * stream );

Write an integer to a stream.
  Writes a binary integer of type int to the stream and increases its position indicator to point to the next character.
  putw assumes no alignment in the stream.
  Notice that the length of int type depends on systems and compilers so this may cause some porting problems.


integer to be written.
Pointer to an open file.

Return Value.
  On success the integer value written is returned.
  On error EOF is returned  

  Not defined in ANSI-C, but supported by many compilers.
  Notice that the size of int may vary depending on system.


/* putw/getw example */
#include <stdio.h>

  FILE * pFile;
  int i;

  pFile = fopen ("myfile.bin","wb+");
  putw (20,pFile);
  rewind (pFile);
  i=getw (pFile);
  printf ("The double of %d is %d.\n", i, i*2);
  return 0;
This example creates a binary file called myfile.bin and stores a numeric value on it. Then this value is retrieved and its double is printed. This is done to emphatize that the type used is of type int and not a string.

See also.
  getw, printf, fwrite

© The C++ Resources Network, 2000