getw
int  getw ( FILE * stream );
stdio.h
  cplusplus.com  

Get the next int value from a stream.
  Returns the next value of type int from the stream and increases the position indicator of the stream to point the next character.
  getw assumes no alignment in the stream and is not intended for use with files opened in text mode.
  Notice that the length of int type depends on systems and compilers so this may cause some porting problems.

Parameters.

stream
Pointer to an open file.

Return Value.
  On success the integer read is returned.
  If the End Of File is reached or there has been an error reading, the function returns an EOF character.
  Because EOF is a valid integer, use feof() or ferror() to verify if an error has occurred or the End-Of-File has been reached.
 

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

Example.

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

main()
{
  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.
  getc, putw, fread, fscanf


© The C++ Resources Network, 2000