从流中获取整数。Gets an integer from a stream.


int _getw(   
   FILE *stream   


指向 FILE 结构的指针。Pointer to the FILE structure.

返回值Return Value

_getw 返回读取的整数值。_getw returns the integer value read. EOF 的返回值指示错误或文件尾。A return value of EOF indicates either an error or end of file. 但是,由于 EOF 值也是一个合法的整数值,因此使用 feofferror 验证文件尾或错误条件。However, because the EOF value is also a legitimate integer value, use feof or ferror to verify an end-of-file or error condition. 如果 streamNULL,则将调用无效的参数处理程序,如参数验证中所述。If stream is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许继续执行,则将 errno 设置为 EINVAL 并且该函数将返回 EOFIf execution is allowed to continue, errno is set to EINVAL and the function returns EOF.


_getw 函数从与 int 关联的文件中读取类型 stream 的下一个二进制值,并递增关联的文件指针(如果有)以指向下一个未读字符。The _getw function reads the next binary value of type int from the file associated with stream and increments the associated file pointer (if there is one) to point to the next unread character. _getw 未对流中的项采用任何特定的对齐方式。_getw does not assume any special alignment of items in the stream. 使用 _getw 可能导致出现移植问题,因为 int 类型的大小和 int 类型中的字节顺序因系统而异。Problems with porting can occur with _getw because the size of the int type and the ordering of bytes within the int type differ across systems.


例程Routine 必需的标头Required header
_getw <stdio.h><stdio.h>

有关更多兼容性信息,请参见“简介”中的 兼容性For more compatibility information, see Compatibility in the Introduction.


// crt_getw.c  
// This program uses _getw to read a word  
// from a stream, then performs an error check.  

#include <stdio.h>  
#include <stdlib.h>  

int main( void )  
   FILE *stream;  
   int i;  

   if( fopen_s( &stream, "crt_getw.txt", "rb" ) )  
      printf( "Couldn't open file\n" );  
      // Read a word from the stream:  
      i = _getw( stream );  

      // If there is an error...  
      if( ferror( stream ) )  
         printf( "_getw failed\n" );  
         clearerr_s( stream );  
         printf( "First data word in file: 0x%.4x\n", i );  
      fclose( stream );  

输入:crt_getw.txtInput: crt_getw.txt

Line one.  
Line two.  


First data word in file: 0x656e694c  

请参阅See Also

流 I/O Stream I/O