home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / getw.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  2KB  |  62 lines

  1. /***
  2. *getw.c - read a binary word from a stream
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _getw() - gets a binary integer from a stream
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <stdio.h>
  13. #include <dbgint.h>
  14. #include <file2.h>
  15. #include <internal.h>
  16. #include <mtdll.h>
  17.  
  18. /***
  19. *int _getw(stream) - read an int from a stream
  20. *
  21. *Purpose:
  22. *       get n bytes (n=sizeof(int)); OR them together in proper order; high
  23. *       byte first. check for EOF between getc's.
  24. *       this routine should be machine independent.
  25. *
  26. *Entry:
  27. *       FILE *stream - stream to read integer from
  28. *
  29. *Exit:
  30. *       returns the int read from the stream
  31. *       returns EOF if fails (but this is a legit int value, so
  32. *       should test feof() or ferror().
  33. *
  34. *Exceptions:
  35. *
  36. *******************************************************************************/
  37.  
  38. int __cdecl _getw (
  39.         FILE *str
  40.         )
  41. {
  42.         REG1 FILE *stream;
  43.         REG2 int bytecount = sizeof(int);
  44.         int word;
  45.         char *byteptr = (char *)&word;
  46.         int retval;
  47.  
  48.         _ASSERTE(str != NULL);
  49.  
  50.         /* Init stream pointer */
  51.         stream = str;
  52.  
  53.         _lock_str(stream);
  54.  
  55.         while (bytecount--)
  56.                 *byteptr++ = (char)_getc_lk(stream);
  57.         retval = ((feof(stream) || ferror(stream)) ? EOF : word);
  58.  
  59.         _unlock_str(stream);
  60.         return(retval);
  61. }
  62.