1 // 2 // putw.cpp 3 // 4 // Copyright (c) Microsoft Corporation. All rights reserved. 5 // 6 // Defines _putw(), which writes a binary integer to a stream. 7 // 8 #include <corecrt_internal_stdio.h> 9 10 11 12 // Writes the binary int value to the stream, byte-by-byte. On success, returns 13 // the value written; on failure, returns EOF. Note, however, that the value may 14 // be EOF--that is, after all, a valid integer--so be sure to test ferror() and 15 // feof() to check for error conditions. 16 extern "C" int __cdecl _putw(int const value, FILE* const stream) 17 { 18 _VALIDATE_RETURN(stream != nullptr, EINVAL, EOF); 19 20 int return_value = EOF; 21 22 _lock_file(stream); 23 __try 24 { 25 char const* const first = reinterpret_cast<char const*>(&value); 26 char const* const last = first + sizeof(value); 27 for (char const* it = first; it != last; ++it) 28 { 29 _fputc_nolock(*it, stream); 30 } 31 32 if (ferror(stream)) 33 __leave; 34 35 return_value = value; 36 } 37 __finally 38 { 39 _unlock_file(stream); 40 } 41 __endtry 42 43 return return_value; 44 } 45