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

  1. /***
  2. *putw.c - put a binary int to output stream
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _putw() - puts a binary int to an output 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 _putw(word, stream) - write a binary int to an output stream
  20. *
  21. *Purpose:
  22. *       Writes sizeof(int) bytes to the output stream, high byte first.
  23. *       This routine should be machine independent.
  24. *
  25. *Entry:
  26. *       int word - integer to write
  27. *       FILE *stream - stream to write to
  28. *
  29. *Exit:
  30. *       returns the word put to the stream
  31. *       returns EOF if error, but this is a legit int value, so should
  32. *       test with feof() or ferror().
  33. *
  34. *Exceptions:
  35. *
  36. *******************************************************************************/
  37.  
  38. int __cdecl _putw (
  39.         int word,
  40.         FILE *str
  41.         )
  42. {
  43.         REG1 FILE *stream;
  44.         REG3 int bytecount = sizeof(int);
  45.         REG2 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.         {
  57.                 _putc_lk(*byteptr,stream);
  58.                 ++byteptr;
  59.         }
  60.         retval = (ferror(stream) ? EOF : word);
  61.  
  62.         _unlock_str(stream);
  63.         return(retval);
  64. }
  65.